I am looking to create a generic method that receives a List. I need to determine what the object is within the list so that I can then do the necessary work according to the object that is being pa_Stack Overflow日本語サイト
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I am looking to create a generic method that receives a List. I need to determine what the object is within the list so that I can then do the necessary work according to the object that is being pa
I am looking to create a generic method that receives a List. I need to determine what the object is within the list so that I can then do the necessary work according to the object that is being passed in within the list. How can I determine the kind of object that the List contains?
well you have the is keyword, which can compare objects
is
if(myObject is MyClass) doStuff();
you also have
typeof(myObject);
and as L.B. said, you have
obj.GetType() too
isオブジェクトを比較できるキーワードがあります
あなたも持っています
そしてLBが言ったように、あなたは持っています
given
List<T> objects;
you can get the type like this
var objType = typeof(T);
If the object type is not one of the generic parameters of your method then you can use the Type.GetGenericArguments method that returns the types of the parameters of a generic type (in your case List).
Else if the object type is one of the generic parameters of your method then use Travis J's answer.