Click here to Skip to main content
12,398,000 members (45,546 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Say we have a the following generic class:
public class Node<T> {}

But we need to pass this to a non generic method in a none generic class :
public class classname
{ 
    public void SomeMethod(Node?? node) { }
}

How can this be done?
Posted 7-Dec-11 20:27pm
Mehdi Gholam314.2K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Mehdi,

Answering this question is quite a trivial thing. You could answer by yourself, you just need to be logical.

You need to start with non-generic method, as this is the most limiting (by the way, why it is not generic? OK, I understand why it could be). What variants do we have?

First, it could expect any fixed parameter type; in this case, it will determine the generic parameter:

public class Node<T> {/*...*/}
 
public class classname
{ 
    public void SomeMethod(Parameter node) {/*...*/}
}
 
public class Parameter {/*...*/}
 
//...

Node<Parameter> parameter = ...
classname instance = ...
instance.SomeMethod(parameter); //isn't it?

Another variant is using wider type. Let starts with the widest, which is System.Object:

public class Node<T> {/*...*/}
 
public class classname
{ 
    public void SomeMethod(System.Object node) {/*...*/}
}
 
public class Parameter {/*...*/} //this is any type again, always is System.Object

//...

Node<Parameter> parameter = ...
classname instance = ...
instance.SomeMethod(parameter); //same thing

And finally, the same case as above but with generic constraint; the idea is to use the widest common type instead of System.Object:

public abstract class AbstractParameter {/*...*/}
 
public class Node<T> where T: AbstractParameter {/*...*/}
 
public class classname
{ 
    public void SomeMethod(AbstractParameter node) {/*...*/}
}
 
public class Parameter : AbstractParameter {/*...*/} //this time, any type but derived from AbstractParameter

//...

Node<Parameter> parameter = ...
classname instance = ...
instance.SomeMethod(parameter); //isn't it?

Also, there is a variant with interface instead of abstract class, essentially the same.

That's all, it looks like.

—SA
  Permalink  
v2
Comments
Mehdi Gholam 8-Dec-11 3:00am
   
Actually my problem is a little more complicated, I have a generic delegate in the Node which I need to call in the non generic class.
SAKryukov 8-Dec-11 10:33am
   
Yes, I felt that while writing this. You should have given some idea on the purpose of it; as is the question did seem to have much practical meaning to me, so I just had to answer formally. In the case of the delegate the situation is the same: the generic parameter is defined be the requirement of non-generic. I hope this answer will at least help you to come the right idea. If not 00 -- you would have to ask again.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The generic class could inherit from a non-generic class.
public class NodeBase
{
    // Base class stuff here

    public virtual void NodeMethod()
    {
        // Do something
    }
}
 
public class Note<t> : NodeBase
{
    // Generic class stuff here

    public override void NodeMethod()
    {
        // Do something special
    }
}
Then you can hand it over to the method this way:
public class classname
{
    public void SomeMethod( NodeBase node)
    {
        // Use methods declared in base class
        node.NodeMethod();
    }
}
  Permalink  
v2
Comments
Mehdi Gholam 8-Dec-11 2:40am
   
Nice 5'ed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 8 Dec 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100