Click here to Skip to main content
15,845,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Say we have a the following generic class:
C#
public class Node<T> {}


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


How can this be done?
Posted

The generic class could inherit from a non-generic class.
C#
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:
C#
public class classname
{
    public void SomeMethod( NodeBase node)
    {
        // Use methods declared in base class
        node.NodeMethod();
    }
}
 
Share this answer
 
v2
Comments
Mehdi Gholam 8-Dec-11 2:40am    
Nice 5'ed.
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:

C#
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:

C#
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:

C#
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
 
Share this answer
 
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.
Sergey Alexandrovich Kryukov 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900