By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

Generics

P: n/a

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
Nov 28 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 28 '05 #2

P: n/a
"Gene Vital" <no********@msnew.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP09.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 28 '05 #3

P: n/a
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("My CustomMethod",new
Type[] { this.GetType() });
if (myCustomMethod!=null)
{
myCustomMethod.Invoke(this.Parent,new object[] { this });
}

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 28 '05 #4

P: n/a
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

"Gene Vital" <no********@msnew.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

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

Nov 28 '05 #5

P: n/a
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
"Gene Vital" <no********@msnew.com> wrote in message
news:#3**************@TK2MSFTNGP09.phx.gbl...

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

Nov 29 '05 #6

P: n/a
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

"Gene Vital" <no********@msnew.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

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

Nov 29 '05 #7

P: n/a
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

"Marc Gravell" <mg******@rm.com> wrote in message
news:eK*************@TK2MSFTNGP15.phx.gbl...
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

"Gene Vital" <no********@msnew.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

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


Nov 29 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.