Generics

  • Thread starter Thread starter Gene Vital
  • Start date Start date
G

Gene Vital

Hi all,

I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?

I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.






An example would be



public class MyClass : UserControl
{

public void SomethingChanged()
{
// this method is bound to an event somewhere

// call the parent class method, I don't care what type the parent
class is just that is has the MyCustomMethod() method.

if (this.Parent.GetType().GetMember("MyCustomMethod").GetLength(0) > 0)
this.parent.MyCustomMethod(this)
}

}


thanks for all the help
 
Gene,
I thought this was what Generics was supposed to be all about but I have
spent the whole day Googling and I can't find a way to do this in C#.

No I don't think generics will help you here. If you author the
controls you can make them implement an interface where the
MyCustomMethod is defined (or derive from a common base class).
Otherwise you need late binding with Reflection.


Mattias
 
"Gene Vital" <[email protected]> a écrit dans le message de %[email protected]...

| I need some help in understanding how to use Generics. I have a class
| based on a user control that can be put on any Container at runtime, I
| want to be able to call a method on the parent class without knowing the
| type of the parent class, can this be done with C#?
|
| I thought this was what Generics was supposed to be all about but I have
| spent the whole day Googling and I can't find a way to do this in C#.

What you describe is not generics, it is polymorphism. Generics allows you
to write strictly typed code that applies to many different types, but that
can be written once only. Each time you use a generic type, you are not
deriving from a base class, you are actually using classes that are siblings
to each other. You cannot assign a parameterised instance of a generic class
to a differently parameterised instance. You must usually know the full type
of a generic instance in order to manipulate it, unless you have
methods/properties declared in a non-generic base class and that are
overridden in the generic derivative.

Joanna
 
I'm afraid generics won't help you much here, but still you're not far from
the solution.
You can use reflection, which you've used in your example, to do this.

MethodInfo myCustomMethod=this.Parent.GetType().GetMethod("MyCustomMethod",new
Type[] { this.GetType() });
if (myCustomMethod!=null)
{
myCustomMethod.Invoke(this.Parent,new object[] { this });
}

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Hi Gene,

No, this is not what Generics are used for. A Generic class is a class that
takes one or more type parameters. When a Generic class is compiled, it uses
the type(s) passed to it, and is strongly typed.

What you're talking about is reflection, which is the discovery of
characteristics of a type. From what I gather from your description, the
only thing known about the container Control for your Control is that it is
a Control, which isn't going to tell you anything about whether or not it
has a member defined that is a Method of the description you're looking for.
Therefore, you need to use reflection to find out.

The code you posted is close to the solution, but makes several fatal
assumptions. First, the return value of Type.GetMember() is an array of
System.Reflection.MemberInfo, but you are treating it as if it is a single
value. All you probably need to know is whether or not the Member Method
exists, and what type to cast the container to in order to properly access
the Member. So, what you need to do is first determine the type:

System.Type t = this.Parent.GetType();

You need to store this in order to use it for casting if the Member exists.
Otherwise, you have to call GetType() twice.

Next, check to see whether the Member exists:

if (t.GetMember("MyCustomMethod")) != null
{
t.InvokeMember(("MyCustomMethod", BindingFlags.InvokeMethod, null,
this.Parent, new Object[] {this});
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 
I don't have vs2005 here to test this, but try this......


public class MyClass <TParent> : UserControl
{
protected SomeEvent(object sender, EventARgs e)
{
TParent parent = this.Parent as TParent;
parent.MyCustomMethod();
}
}

public class MyParent
{
private MyChild<MyParent> child;

public MyCustomMethod() {...}
}

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Another solution - what about events? Especially in the UserControl world,
this would seem to be a fairly "natural" way for a child control to indicate
something to it's parent, without the child having to know anything about a:
the parent, or b: the implementation... so if your child had a simple public
event, the container can subscribe and provide it's own implementation
(based on the event args), and all the child has to do is call the event?

Marc
 
You bring up a good point, Marc. Objects should generally "mind their own
business." This means that a child object should generally not be calling
methods in a container. Raising an event is an excellent idea. that way, the
container can handle (or not) the event itself.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 

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

Back
Top