简体   繁体   中英

use a generic type as parameter in C#

is there a method to tell a method which type a generic has? what i want to do is to tell the method it can be only an object of type A or B but nothing else, so i can work within like

if (myObject.GetType() == typeof(myTypeA)){doAstuff();} if (myObjectGetType() == typeof(myTypeB)) {doBstuff();}

method<T>(T myObject){ T = myTypeA, T = myTypeB, T = nothing else}

thanks for any help

You can restrict the allowed types for a gernic with the where command:

public void Test<T>(T param) where T : TypeA {
  ...
}

https://docs.microsoft.com/de-de/dotnet/csharp/language-reference/keywords/where-generic-type-constraint

But this are only simple constraints so it does not solve the problem for two classes but for this case you can use method overloading:

public void Test(TypeA param) {
  ...
}
public void Test(TypeB param) {
  ...
}

If you have only two classes I think that is the best solution because generics would have no benefits.

You could check for the type inside the method, then cast it to the appropriate type and do the appropriate "stuff":

public void method<T>(T myObject)
{
    if (myObject is myTypeA)
    {
        myTypeA objA = myObject as myTypeA;
        objA.DoA_Stuff();
    }
    else if (myObject is myTypeB)
    {
        myTypeB objB = myObject as myTypeB;
        objB.DoB_Stuff();
    }
    else
    {
        return ;
    }
}

But that would be a waste of generics. If they share some methods you could also make a base class, and let typeA and typeB inherit from it. Then your method could take a base class object as parameter:

public void method(BaseClass myObject)

and there would be only one if - case and one casting. Only the one with more methods then the base class.

EDIT:

Imagine you would have such a structure:

public class BaseType
{
    public int SharedProp { get; set; } // shared property 

    public virtual int DoSharedStuff() // shared method
    {
        return SharedProp;
    }    
}

public class myTypeA : BaseType
{
    public int A_Prop { get; set; }

    // overwritten shared meth adjusted to the needs of type A
    public override int DoSharedStuff() 
    {
        return base.SharedProp + this.A_Prop;
    }
}

public class myTypeB : BaseType
{
    public int B_Prop { get; set; }

    // overwritten shared meth adjusted to the needs of type B
    public override int DoSharedStuff()
    {
        return base.SharedProp + this.B_Prop;
    }

    // individual method of Type B
    public int DoB_Stuff()
    {
        return this.B_Prop;
    }
}

Then you method would take only one of the children of the base class and execute according to the needs:

public void method(BaseType myObject)
{
    // shared case: here type A will perform type A action 
    // and type B will perform type B action
    myObject.DoSharedStuff();

    // case B where you need really the extra stuff!
    if (myObject is myTypeB)
    {
        myTypeB objB = myObject as myTypeB;
        objB.DoB_Stuff();
    }
}

This approach or phenomenon is called Polymorphism

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM