Cast generic type parameter to a specific type in C#

后端 未结 7 978
终归单人心
终归单人心 2021-01-01 17:42

If you need to cast a generic type parameter to a specific type we can cast it to a object and do the casting like below,

void SomeMethod(T t)
{
    SomeClas         


        
相关标签:
7条回答
  • 2021-01-01 17:56

    A better design is to put a constraint on it that is common between type T and the class you want to expect in your method, in this case SomeClass.

    class SomeConsumer<T> where T : ISomeClass
    {
        void SomeMethod(T t)
        {
            ISomeClass obj2 = (ISomeClass) t;
        }
    }
    
    interface ISomeClass{}
    
    class SomeClass : ISomeClass {}
    

    Edit based on edit of Question

    That is bad design. Try to move that "operation" into the class itself so the caller does not have to know the type. If that is not possible share more of what is being done, what you want to accomplish though is that you do not have a stack of if/else statements where execution depends on the type of object being passed in to the method.

    class SomeConsumer<T> where T : ISomeClass
    {
        void SomeMethod(T t)
        {
            ISomeClass obj2 = (ISomeClass) t;
            // execute
            t.Operation();
        }
    }
    
    interface ISomeClass{
        void Operation();
    }
    
    class SomeClass : ISomeClass {
        public void Operation(){/*execute operation*/}
    }
    
    0 讨论(0)
  • 2021-01-01 17:57

    I know this is a late question but here what you can do

    A great option is to make your function accept parameter of class object, and do your switch case as you wish

    And just do the casting

    YourClass = (YourClass) parameterObject;
    
    0 讨论(0)
  • 2021-01-01 18:02

    If there is no relation between the input type T and the target types TypeA or TypeB (using parameter contraints), and we are looking purely at the casting-problem, the answer is simple:

    No, there is no better way than the method you are using!!!

    I do agree with some other people, if you are doing more operations on that object, you might wanna choose a different design.

    0 讨论(0)
  • 2021-01-01 18:03

    Using as:

    SomeClass obj2 = t as SomeClass;
    

    This would not throw an exception and t would be null if the cast fails.

    I don't really know what you're trying to do, but I hope that you're not missing the point of Generics here.

    If your intention is to restrict the method to type SomeClass and descendants:

    void SomeMethod(T t)  where T : SomeClass
    
    0 讨论(0)
  • 2021-01-01 18:14

    You can use Convert.ChangeType

    SomeClass obj2 = (SomeClass)Convert.ChangeType(t, typeof(SomeClass));
    

    Although, keep in mind that this will throw an exception if a cast is invalid.

    0 讨论(0)
  • 2021-01-01 18:15

    You can use as for that case

    void SomeMethod(T t)
    {
        SomeClass obj2 = t as SomeClass;
    }
    
    0 讨论(0)
提交回复
热议问题