Reputation: 149
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...
Upvotes: 12
Views: 76779
Reputation: 19130
List<object>
is a different type than e.g. List<int>
. That's why your cast fails.
Depending on what your goal is, you should choose a different solution. It seems your method needs to be able to handle lists and normal objects.
List<T>
does implement IEnumerable
, which allows you to enumerate all elements.
public static void MyMethod( object _anyObject )
{
IEnumerable list = _anyObject as IEnumerable;
if ( list != null )
{
foreach ( var item in list )
{
// Do whathever you want.
}
}
else
{
// Probably just a plain object, not a collection.
}
}
Upvotes: 3
Reputation: 20312
So in my method it accept the parameter as Object, then I will determine what to do by their type
It sounds like you want to use the is
keyword:
if (_anyObject is List<AnyClass>)
{
List<AnyClass> list = (List<AnyClass>)_anyObject;
}
else if (_anyObject is ...)
{
}
Prefer using method overloads, though.
Upvotes: 0
Reputation: 5437
It sounds like you should use method overloading instead of type detection. This will also break your single large method up into smaller, more maintanable methods.
public static void MyMethod(List<AnyClass> list)
{
foreach (var obj in list)
{
MyMethod(obj);
}
}
public static void MyMethod(AnyClass single)
{
//work with single instance
}
Upvotes: 1
Reputation:
If you want a generic method that performs actions on different types, make a real generic method:
public static void MyMethod<SomeType>(SomeType _anyObject)
Your question indicates that you really want a method to do different operations to different types, instead of the same operation. You might want to reconsider such a design by using method overloads instead.
public static void MyMethod(List<AnyClass> list)
public static void MyMethod(int anyInt)
public static void MyMethod(string someString)
And so on for the types you want.
Upvotes: 1
Reputation: 15242
You could use Generics
to make use of this
public static void myMethod<T>(T object)
{
T yourObject = object;
if (yourObject is ICollection)
{
//Do your iteration
}
}
Upvotes: 4
Reputation: 156654
The generic List class implements the non-generic IList interface, so you can say:
IList objectList = obj as IList;
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.
Upvotes: 17