Reflection ?

J

Jarod

Hi
I have let's say 10 classes and I need a function that will call a method on
them. But this method name is the same in each of this class. For this
method class can be initiated by constructor without params. Is there a
simple way to invoke Delete() function in a given class just by giving it
name ? Other than using reflection ?
Function will look like this:
void deleteSomeThing(string className)
{
// some code that will do : className.Delete();
}
Jarod
 
A

Aboulfazl Hadi

Hi Jarod
I think it is better to use polymorphism ( by interface or by
inheritance) to invoking a method with different implementation.

I hope this helps
A.Hadi
 
V

vj

I don't think so, to my knowledge.. Would be interested in knowing if there
is any available

VJ
 
T

Tim Wilson

Are you looking to call the Delete method against an object or the class
itself? In other words, is the Delete method static?
 
J

Jarod

Are you looking to call the Delete method against an object or the class
itself? In other words, is the Delete method static?

--

It can be if it makes diffrence ;) Or makes it easier ;)
Jarod
 
T

Tim Wilson

From the code that you posted (which may have been pseudo code) it makes it
look like it could be a static method. I wasn't sure. I was actually
thinking that if you're using objects then this would be a classic use of
polymorphism. Or, for some reason, do you only have the name of the class as
a string?
http://msdn2.microsoft.com/en-us/library/ms173152(VS.80).aspx

private void button1_Click(object sender, EventArgs e)
{
DerivedClassA a = new DerivedClassA();
DerivedClassB b = new DerivedClassB();
Delete(a);
Delete(b);
}

private void Delete(BaseClass c)
{
c.Delete();
}

....

public abstract class BaseClass : System.Object
{
public virtual void Delete()
{
}
}

public class DerivedClassA : BaseClass
{
public override void Delete()
{
MessageBox.Show(this.ToString());
}
}

public class DerivedClassB : BaseClass
{
public override void Delete()
{
MessageBox.Show(this.ToString());
}
}
 
J

Jarod

From the code that you posted (which may have been pseudo code) it makes
it
look like it could be a static method. I wasn't sure. I was actually
thinking that if you're using objects then this would be a classic use of
polymorphism. Or, for some reason, do you only have the name of the class
as
a string?

There is 10 really diffrent classes but they have one common thing ;) The
delete funcion ;) And I chose to invoke it by late binding ;) Just to learn
something new.
Jarod
 
T

Tim Wilson

If all the classes share a common custom base class then you can use the
polymorphic approach. You could also define an interface and have all the
classes implement that interface to force the implementation of a Delete
method. Either way what you'd be trying to do is guarantee that the object
that is passed to the method has a Delete method. Otherwise, you'd need to
use reflection to look up if the type of the object passed into the method
actually has a Delete method that can be invoked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top