Reputation: 501
I have a requirement where I can get the following in an object -
a type T or List<T>
Converting object into T is easy. How can I convert it to List(by first checking that it can be converted successfully or not), reason I want to convert is to scroll through the list and call tostring on each element.
My actual code -
namespace Generic_Collection_Code
{
class Program
{
public static string DumpObj(object obj)
{
string sTemp = String.Empty;
List<int> ints = obj as List<int>;
if (ints != null)
{
foreach (int i in ints)
sTemp += i.ToString() + ",";
sTemp.Trim(',');
}
else
{
List<string> strings = obj as List<string>;
if (strings != null)
{
foreach (string s in strings)
sTemp += s + ",";
sTemp.Trim(',');
}
else
{
sTemp += obj.ToString();
}
}
return sTemp;
}
static void Main(string[] args)
{
List<int> listInts = new List<int>();
listInts.Add(1);
listInts.Add(2);
listInts.Add(3);
Console.WriteLine("Object1: {0}", DumpObj(listInts));
int i = 90;
Console.WriteLine("Object2 {0}", DumpObj(i));
List<string> listStrings = new List<string>();
listStrings.Add("1");
listStrings.Add("2");
listStrings.Add("3");
Console.WriteLine("Object3: {0}", DumpObj(listStrings));
Console.ReadKey();
}
}
}
The above code works but I know its an ugly way to achieve this. I wanted to ask from community how can I have this function like -
public static string DumpObj<T>(object obj)
{
string sTemp = String.Empty;
List<T> list = obj as List<T>;
if (list != null)
{
foreach (T i in list)
sTemp += i.ToString() + ",";
sTemp.Trim(',');
}
return sTemp;
}
This gives me compilation errors as I have to specify T while calling DumpObj with error as -
Error 1 The type arguments for method 'Generic_Collection_Code.Program.DumpObj(object)' cannot be inferred from the usage. Try specifying the type arguments explicitly. D:\DotNet\Generic_Collection_Code\Generic_Collection_Code\Program.cs 57 47 Generic_Collection_Code
as you can see, obj is an object, i dont know its type while calling dumobj.
I hope I have made myself clear on this one.
I appreciate your time!
Regards Amit
Upvotes: 6
Views: 28420
Reputation: 1116
How about casting the Object into System.Collections.IList (instead of the Generic version) because Generic list also implement this interface. Then cast each of them into the desired type. Here is what I am working on..
private static void DataSourcePropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs args) {
BarChart _ = sender as BarChart;
if (args.Property.Equals(BarChart.DataSourceProperty)) {
System.Collections.IList data = (System.Collections.IList)args.NewValue;
if (data == null) return;
foreach (object __ in data) {
IChartDataItem item = __ as IChartDataItem;
BarChartItem bar = new BarChartItem() {
Label = item.Label,
Value = item.Value
};
_._visualCollection.Add(bar);
if (_.MaxData < item.Value)
_.MaxData = item.Value;
}
if (_.Orientation == Orientation.Horizontal)
_.Ratio = _.Width / _.MaxData;
}
}
Upvotes: 0
Reputation: 3731
How about combining "as" with "is"?
if (object is List<T>)
{
List<T> genericlist = object as List<T>;
// loop list
}
else if (object is T)
{
// do something else
}
Upvotes: 0
Reputation: 2748
you cant do this
List<T> genericList = (List<T>)object
might be you want
List<T> genericList = (List<T>)obj
where obj is object
Upvotes: 4
Reputation: 17427
Say
List<T> genericList = object as List<T>;
if(genericList != null)
{
// Do the loop
}
The "as" keyword verifies that "object" actually "is-a" List< T >. If so, you get a List< T > back from it. If not, you get null.
Upvotes: 11
Reputation: 4468
What is the compilation error you're getting? If T is declared as a generic type parameter in your context then then the only compile-time issue I can see with that statement is the use of the keyword object
as a variable name. At any rate, I'd suggest something like this as best expressing your intention:
IEnumerable enumerable = obj as IEnumerable;
if (enumerable != null)
{
foreach (object item in enumerable)
{
sTemp += item.ToString();
}
}
You may also want to consider using a StringBuilder if your list is likely to have a lot of items.
Upvotes: 9