0

I inherited from the generic version of list to add some properties to anhother generic class in asp.net core then I have a method that receive an object parameter. In some point of the workflow of the framework, it convert my class to an object and pass it to this method. So, I need to cast or parse the object to a specific class of my inherited class.

    public class ClassList<T> : List<T>
    {
        public int SomeProperty {get; set;}
        ....
    }
    public void SomeMethod(object value){

    }

I need to cast the object in the SomeMethod method to an specific type of the generic class that I've created.

Something like this:

    ClassList<OtherClass> otherList = (ClassList<OtherClass>) object;

The problem is how to know the type of the generic type and how to convert the object to this type at runtime?

julian zapata
  • 89
  • 1
  • 8
  • 1
    99% of the time the best way to know the type of `value` is to change `SomeMethod(object value)` to `SomeMethod(SomeSpecificType value)`. Now you know what type `value` is. It's `SomeSpecificType`. There will be other literal (and correct) answers telling you how to do what you're asking, but you'll likely gain a lot more from learning why this isn't a good problem to have and how to avoid having it in the first place. – Scott Hannen Jun 10 '19 at 17:18
  • The SomeMethod is the WriteJson method of JsonConverter. I can't change the signature. I'm trying to make a Custom Converter to an API. – julian zapata Jun 10 '19 at 17:24
  • Please refer this question: https://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class but remember Reflection may has a cost on performance in your code. – Flavio Francisco Jun 10 '19 at 17:27
  • It might help to clarify the question - and add some more code - so that someone can visualize the relationship between the classes you're describing. With one class, one method, and one line of code it's hard to piece together what you're describing. I'm not saying the information isn't there, but it requires someone to read over it a few times and then guess which code refers to which part of the question, and possibly guess wrong. – Scott Hannen Jun 10 '19 at 17:28

1 Answers1

0

No, because it has been boxed, only the runtime can dynamically know what the class is.

So SomeMethod can't know what a generic class is before compiling

eg:

thie code will get error :

CS0411 The type arguments for method 'UserQuery.SomeClass.SomeMethod(UserQuery.ClassList)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

public class Program
{
    public static void Main()
    {
        object datas = new ClassList<string>() { "github", "microsoft" };

        var result = SomeClass.SomeMethod(datas);//Error :  CS0411 The type arguments for method 'UserQuery.SomeClass.SomeMethod<T>(UserQuery.ClassList<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
    }
}

public class ClassList<T> : List<T>
{
    public int SomeProperty { get; set; }
}

public class SomeClass
{
    public static ClassList<T> SomeMethod<T>(ClassList<T> value)
    {
        ClassList<T> otherList = (ClassList<T>)value;
        return otherList;
    }
}
Wei Lin
  • 3,591
  • 2
  • 20
  • 52