I am trying to make a generic method that can accept any object, in which, even the object is List<AnyClass>.
Something like:
public static void MyMethod(object _anyObject)
{
}
So in my method it accept the parameter as Object, then I will determine what to do by their type to do
So when I know that’s a List type, I want to convert that Object back to List<AnyClass>, iternate each object and their propertie
I tried to do:
List<object> ObjectList = object as List<object>;
But it will return null, because the object originally is List<AnyClass>
Thanks in advance
================
Edit: Sorry, seems like I haven’t make it clear, because I simplifed the problem…
I means the Object might be:
_anyObject = List<Class1>
_anyObject = Class2
_anyObject = DataSet
_anyObject = AnyClass
_anyObject = List<object>
So say, the user can even put in a List<object>, but each of the object in that list can be different class..That’s why I can’t use <T>.
The original of my problem is:
public static void MyMethod(List<object> _anyList)
{
}
Then I don’t care what they put into the list, even the object in the list contains another object list…
The generic List class implements the non-generic IList interface, so you can say:
However, what you’re trying to do is generally considered bad practice as it eliminates any semblance of type safety. If you explain in what context you’re using this, we might be able to suggest a better alternative, like using a generic method.