.Net dynamic list conversion - c#

I have to do a comparison of 2 lists. The problem is that I don't know of what type the field inside the list are, they can be int, string, decimal, enums or even other objects.
I will know the type only on runtime. I was thinking of creating a list of object and cast them to object the problem is that let's say I have a List<int> and I'm trying to cast it to object it fails.
Another problem is that I know there is a list only on runtime. so on runtime I need to transform the variable of type object to a list.
How can I cast that object to List and how can I cast it to let's say list of objects?
Update:
I have and object and by reflection I'm getting the the property of it with
var oldProperty = property.GetValue(old);
var newProperty = property.GetValue(new);
Once I have the properties values and I can see it's a list I will need to compare those 2. Let's say oldProperty is of type List
I've tried to do something like:
var myOldList = (List<object>)oldProperty;
If the cast fails with
Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'System.Collections.Generic.List`1[System.Object]'
Here you have a look of the function i;m trying to create. Please don't mind of null objects(is not in the scope)
public void SetDifference(object first, object second)
{
var properties = first.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
var oldValue = property.GetValue(first);
var newValue = property.GetValue(second);
if (Convert.GetTypeCode(newValue) != TypeCode.Object)
{
if (!oldValue.Equals(newValue))
{
result.AddDifference(new PrimitiveComparison()
{
BeforeValue = oldValue.ToString(),
AfterValue = newValue.ToString(),
PropertyName = property.Name
});
}
}
else
{
if (property.PropertyType.Name.Contains("List"))
{
// here fails with the error from above
var oldList = (List<object>)oldValue;
var newList = (List<object>)newValue;
if (oldList.Count != newList.Count)
{
result.AddDifference(new PrimitiveComparison()
{
BeforeValue = oldList.Count.ToString(),
AfterValue = newList.Count.ToString(),
PropertyName = property.Name + "Count"
});
}
// add the list differences
result.AddDifference(SetListDifference(oldList, newList);
}
else
{
var diffrence = SetDifference(oldValue, newValue);
if (!diffrence.areEqual)
{
result.AddDifference(diffrence);
}
}
}
}
}

You can just cast your two values to IList and compare them, for example like this:
static bool AreEqual(IList first, IList second) {
if (first.Count != second.Count)
return false;
for (int i = 0; i < first.Count; i++) {
if (!object.Equals(first[i], second[i]))
return false;
}
return true;
}

once you do conversion of you list than you can check both the list have same element or not by using except method of linq, to find both are equal or not
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);
if(onlyInFirstSet.Count() ==0)
Console.WriteLine("equal");
for primitive type this works fine but for user defined type you need compare implementation.
Check this blog post for comparing list of two different type : Difference between list of user defined types
If you are aware that is going to be IEnumerable type than you can try
List<object> objlst = (value as IEnumerable<object>).Cast<object>
().ToList()
you can try like this
Type t = typeof(obj);
if (t == typeof(List<int>)) {
var lst= (List<int>)obj;
} else if (t == typeof(List<string>)) {
var lst = (List<string>)obj;
} else if (t == typeof(List<decimal>)) {
var lst = (List<decimal>)obj;
}
else if (t == typeof(List<EnumName>)) {
var lst = (List<EnumName>)obj;
}

Related

Accessing elements of an Array (of any element type) from an object

I use management to access devices properties and I wrote a code below to create an array of dictionaries. my application show properties in a listview control; so I need to convert all properties value to simple string
Dictionary<string,string>[] getInfo(string k) {
// using `k` as management-key
var mos = new ManagementObjectSearcher($"select * from {k}");
var devices = new List<Dictionary<string, string>>();
var mosc = mos.Get(); // mosc is a collection of all devices with same key
foreach (var device in mosc) {
var properties = new Dictionary<string, string>();
foreach (var p in device.Properties) {
if (p.Value != null) {
if (p.IsArray) {
// I have problem in here
// my application must convert p.value to string
var collection = (IEnumerable<object>)p.Value
properties[p.Name] = string.Join(", ", collection.Select(x=>x.ToString()));
} else
properties[p.Name] = p.Value.ToString();
} else properties[p.Name] = "";
}
devices.Add(properties);
}
return devices.ToArray();
}
p.Value type is object but sometimes it contain an array like UInt[] or String[], I found part of code from stackoverflow but it didn't help me and it say:
System.InvalidCastException: 'Unable to cast object of type 'System.UInt16[]' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'.'
I tried code below too, but it say same thing:
int[] array = new int[] { 0, 1, 2 }; // <- I haven't access to `array` in my main problem
object obj=array;
// I only can use `obj`
// `obj` is similar to `p.Value` here
IEnumerable<object> collection = (IEnumerable<object>)obj; // <- this line throws exception!
string output=string.join(", ",collection.Select(x=>x.ToString()));
I also tried this codes:
var collection= p.Value as IEnumerable;
// ^ found this line from stackoverflow
// says: Using the generic type 'IEnumerable<T>' requires 1 type arguments
var collection= p.Value as IEnumerable<object>
// `collection` will be null
var collection= (object[]) p.Value
// says: Unable to cast object of type 'System.Int32[]' (or some something like String[]) to type 'System.Object[]'.
IEnumerable<T> is covariant in T so this would be allowed:
IEnumerable<Giraffes> giraffes = ....
var animals = (IEnumerable<Animal>)giraffes;
So then, why does this not work as well?
var array = new[] { 1, 2, 3 };
var objects = (IEnumerable<object>)array; //will not compile
int extends object, right?
Well, the reason is that type variance in C# is only allowed between reference types; the rule is that variance must preverse identity, and there is no way to cast a value type preserving identity in C#; only reference types can and those types of conversion are called reference conversions:
var animal = (Animal)giraffe;
var o = (object)"Hello";
IFish fish = cod;
//etc.
The bits that make up the object dont change, only the type of the refernece changes, hence the name of the conversion. Note that in the very first example, animals and giraffes are the same object, object.ReferenceEquals(animals, giraffes) will return true; we've only changed the type of the variable referencing it.
In your case, to obtain an IEnumerable<object> from an IEnumerable<someValueType>, you'll have to enumerate and box each item creating a new enumerable of the desired type. In order to do so, you can use the extension method Enumerable.Cast<T>():
IEnumerable<object> objects = array.Cast<object>();
Or do the projection yourself:
IEnumerable<object> objects = array.Select(i => (object)i);
second code:
int[] array = new int[] { 0, 1, 2 };
object obj=array;
var obj_array=(Array)obj;
IEnumerable<object> collection = obj_array.Cast<object>();
string output=string.join(", ",collection.Select(x=>x.ToString()));
so main code will be:
Dictionary<string,string>[] getInfo(string k) {
// using `k` as management-key
var mos = new ManagementObjectSearcher($"select * from {k}");
var devices = new List<Dictionary<string, string>>();
var mosc = mos.Get(); // mosc is a collection of all devices with same key
foreach (var device in mosc) {
var properties = new Dictionary<string, string>();
foreach (var p in device.Properties) {
if (p.Value != null) {
if (p.IsArray) {
var array = (Array)p.Value;
var collection = array.Cast<object>();
properties[p.Name] = string.Join(", ", collection.Select(x=>x.ToString()));
} else
properties[p.Name] = p.Value.ToString();
} else properties[p.Name] = "";
}
devices.Add(properties);
}
return devices.ToArray();
}

Casting ExpandoObject to AnonymousType of T

Im trying to clone an AnonymousType and enconter some strange behavior.
could anyone tell me why and also tell me a solution.
Here is a test code.
public static T DeepClone<T>(this T objectToBeCloned) where T : class
{
// this will always return null.
return Clone(objectToBeCloned) as T;
// if i try, it will work somehow
// var o = Clone(objectToBeCloned) as dynamic;
// if I try, will get an exception cant cast
// ExpandoObject to AnonymousType even thaugh i could cast it to dynamic
// var o = (T)Clone(objectToBeCloned);
}
// the clone method
public static object Clone(this object objectToBeCloned){
object resObject;
if (primaryType.IsAnonymousType()) // dynamic types
{
var props = FastDeepClonerCachedItems.GetFastDeepClonerProperties(primaryType);
resObject = new ExpandoObject();
var d = resObject as IDictionary<string, object>;
foreach (var prop in props.Values)
{
var item = prop.GetValue(objectToBeCloned);
if (item == null)
continue;
var value = prop.IsInternalType ? item : Clone(item);
if (!d.ContainsKey(prop.Name))
d.Add(prop.Name, value);
}
return resObject;
}
dynamic test = { p1="sd" };
var v =test.DeepClone(test);
// v is always null in this case dont understand why
This isn't the cleanest code, but it clones an anonymous type:
var original = new { Name = "Albert", Age = 99 };
var constructor = original.GetType().GetConstructors().First();
var parameters = constructor.GetParameters();
var properties = original.GetType().GetProperties();
var arguments =
(
from pa in parameters
join pr in properties on pa.Name equals pr.Name
select pr.GetValue(original)
).ToArray();
var clone = constructor.Invoke(arguments);
Not much use though as anonymous types are immutable.

cast to a variable type

Is it possible to cast from a Dynamic or an Object to a variable type? The target variable type is one generic class, only the specific data type varies.
I'm looking for abstract solution, but the situation is this: I want a single Linq2SQL command for different Entities - I can GetProperty + GetValue of the EntityFramework Context, but then it must be casted to some exact types, but these come from variable input - names of DB tables.
Example of what I have:
Type NewType = typeof(System.Data.Objects.ObjectSet<>);
NewType.MakeGenericType(new Type[] {"...some type here that's variable accordint to user input - like Person, Address, Contact, ..."});
Object MyObject = Context.GetType().GetProperty("the same variable like above - Person, Address,...", BindingFlags.Public | BindingFlags.Instance).GetValue(Context, null); // this is EntityFramework Context
Example of what I need but don't know how to do it:
NewType CastedObject = (NewType) MyObject;
// or
NewType CastedObject = Convert.ChangeType(MyObject, NewType);
// or
NewType CastedObject = MyObject as NewType;
so I could do this:
(from x in CastedObject where x.ID == ID select x).FirstOrDefault();
For those, who are familiar with PHP, I'm trying to do this:
(from x in Context.$tablename where x.ID == ID select x).FirstOrDefault();
Since NewType is a runtime type, how could you have a variable of compile-time type NewType? The compiler must know the "declared" type of all variables. So what you want is not possible.
Something you can do is cast MyObject to dynamic. Then stuff like foreach (var x in CastedToDynamic) would compile (but no compile-time type checking).
You can't use LINQ on a dynamic expression, though. You will have to rewrite like:
dynamic found = null;
foreach (var x in CastedToDynamic)
{
if (x.ID == ID)
{
found = x;
break;
}
}
Note: Which overload of == to use will be decided at run-time. So even if ID is a (boxed) System.Guid, say, the == will (unbox and) call the overload of == defined inside that struct. So this works.
Something like this might get at what you are looking at (relies heavily on reflection)
List<dynamic> mylist = new List<dynamic>();
dynamic data = new ExpandoObject();
data.Foo = "foo";
data.ID = 1;
mylist.Add(data);
data = new ExpandoObject();
data.Foo = "foobar";
data.ID = 2;
mylist.Add(data);
data = new ExpandoObject();
data.Foo = "foobar2";
data.ID = 2;
mylist.Add(data);
data = new ExpandoObject();
data.Foo = "foobar2";
data.ID = 3;
mylist.Add(data);
int idiminterestedin = 2;
var dynamicselected = mylist.Select(d=>((IDictionary<string,object>)d)).Where(d=>
{
object id;
if (d.TryGetValue("ID",out id))
{
return (id is int) && (int)id == idiminterestedin;
}
return false;
});
foreach (var v in dynamicselected)
{
Console.WriteLine(v["Foo"]);
}
Console.ReadLine();
Though I'd probably skip the ExpandoObject and go straight for a dictionary:
Dictionary<string, object> data = new Dictionary<string, object>();
data["Foo"] = "foo";
data["ID"] = 1;
And then make a generic extension function(s):
static class ExptensionFunction
{
static T GetValueAs<T>(this Dictionary<string, object> obj, string key)
{
return (T)obj[key];
}
static bool TryGetValueAs<T>(this Dictionary<string, object> d, string key, out T value)
{
object id;
if (d.TryGetValue(key, out id))
{
if (id is T)
{
value = (T)id;
return true;
}
}
value = default(T);
return false;
}
static IEnumerable<Dictionary<string, object>> GetValuesWithKey<T>(this List<Dictionary<string, object>> list, string key, T value)
{
return list.Where(d =>
{
T id;
if (d.TryGetValueAs<T>(key, out id))
{
return id.Equals(value);
}
return false;
});
}
}
Inspired by this:
Creating an anonymous type dynamically?
and
Add property to anonymous type after creation

call a generic method with an object of an unknown type

I have this method
public string DictionaryToString<T, U>(Dictionary<T, U> dict)
{
var valueStrings = dict.Select(x => x.Key.ToString() + ": " + x.Value.ToString());
return String.Join("\n", valueStrings);
}
And I have this object that I want to pass into it
if ((value !=null) && value.GetType().IsGenericType &&
value.GetType().GetGenericTypeDefinition() == typeof (Dictionary<,>))
{
var castValue = value as Dictionary<,>; // this cast does not work
return DictionaryToString(castValue);
}
else
{
return value.ToString();
}
I can use reflection code like this in .Net 4.5
var targetMethodInfo = typeof(MyType).GetMethod("DictionaryToString");
var valueTypeArgs = value.GetType().GenericTypeArguments;
var genericMethod = targetMethodInfo.MakeGenericMethod(valueTypeArgs);
var result = genericMethod.Invoke(this, new[] {value });
return result.ToString();
ButType.GenericTypeArguments is new in .Net 4.5. So how can I do that cast in .Net 4.0?
You are only calling ToString on the key and value, so simply have this method take an IDictionary (non-generic), you are not using anything in there that is type-specific to T or U.
You can then simply cast all arguments to IDictionary:
var d = arg as IDictionary;
if (d != null)
{
var res = DictionaryToString(d);
}
You may also need to amend the DictionaryToString implementation:
static string DictionaryToString(IDictionary d)
{
var vals = new List<string>();
foreach (DictionaryEntry de in d)
{
vals.Add(de.Key.ToString() + ": " + de.Value.ToString());
}
return String.Join("\n", vals);
}
Alternatively, if you really want to use LINQ, you could try casting to dynamic (it isn't possible to cast to anything else as this could be a generic dictionary (KeyValuePair<>) or non-generic hashtable (DictionaryEntry)):
var valueStrings = d.Cast<dynamic>().Select(de => de.Key.ToString() + ": " + de.Value.ToString());
return string.Join("\n", valueStrings);
This basically "duck types" the existence of the Key and Value properties.
This sounds like it might be a case for the old System.Collections namespace:
private static string DictionaryToString(IDictionary dict) {
if (null == dict) throw new ArgumentNullException("dict");
var valueStrings = new List<string>();
foreach (DictionaryEntry item in dict) {
valueStrings.Add(item.Key + ": " + item.Value);
}
return string.Join("\n", valueStrings.ToArray());
}
private static string Test(object value) {
var dict = value as IDictionary;
if (dict != null) {
return DictionaryToString(dict);
}
if (value == null) {
return null;
}
return value.ToString();
}
private static void Main(string[] args) {
var aDictionary = new Dictionary<int, string> {
{ 1, "one" },
{ 2, "two" },
{ 3, "three" }
};
Console.WriteLine(Test(aDictionary));
var anotherDictionary = new Dictionary<string, object> {
{ "one", 1 },
{ "two", "2" },
{ "three", new object() }
};
Console.WriteLine(Test(anotherDictionary));
Console.ReadLine();
}
Reasoning:
The non-generic IDictionary will be a collection of key-value pairs in which the key is an object and the value is an object. All instances of object support ToString, so all keys and values of the collection can be converted to a string without knowing their specific types.
The reason why this does not work:
var castValue = value as Dictionary<,>
is because the generic type Dictionary<TKey, TValue> requires 2 type arguments. Without those type arguments, the collection is not generic. You'd be better off using the non-generic IDictionary if you do not know the key or value types at compile-time.
GetGenericTypeDefinition is available for previous versions http://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition(v=vs.100).aspx. I think the 4.5 page on MSDN is missing the "Other versions" dropdown.
You could cast to dynamic in .NET 4.0. The prior typeof(Dictionary<,>) check will ensure that you won't get runtime errors.
var castValue = value as dynamic;
return DictionaryToString(castValue);
Why not?
if ((value !=null) && value.GetType().IsGenericType &&
value.GetType().GetGenericTypeDefinition() == typeof (Dictionary<,>))
{
return DictionaryToString(castValue);
}
else
{
return value.ToString();
}

Cast an object into generic list

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
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.
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.
you cant do this
List<T> genericList = (List<T>)object
might be you want
List<T> genericList = (List<T>)obj
where obj is object
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
}
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;
}
}

Categories