I read this topic and apply to my code.
//listSort contains ExpandoObjects
List<dynamic> listSort = new List<dynamic>(listDynamic.Count);
foreach (var item in listDynamic)
{
dynamic objDynamic = OrderBySpecialCharacter.ConvertToExpand(item);
string sortValue = OrderBySpecialCharacter.GetValueInExpandoObject(objDynamic, colSortText);// "Name");
objDynamic.Sort = OrderBySpecialCharacter.ConvertToACSII(sortValue.ToUpper());
listSort.Add(objDynamic);
}
List<dynamic> sortedList = new List<dynamic>();
if (colSort2 == null && colSort3 == null)
{
//Error in below line
sortedList = listSort.OrderBy(x=> x.GetReflectedPropertyValue("Sort"),new MrFourCompare<string>()).ToList();
}
----------------
public static ExpandoObject ConvertToExpand<T>(T objInput)
{
ExpandoObject objExpand = new ExpandoObject();
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
IDictionary<String, Object> iExpand = ((IDictionary<String, Object>)objExpand);
foreach (PropertyDescriptor prop in properties)
iExpand.Add(prop.Name, prop.GetValue(objInput));
return (ExpandoObject)iExpand;
}
--------------------
And I see error:
Severity Code Description Project File Line Suppression State
Error CS0411 The type arguments for method
'Enumerable.OrderBy(IEnumerable, Func, IComparer)' cannot be inferred from the usage. Try
specifying the type arguments
explicitly. DemoDynamicObject C:\Users\MrFour-IT\Desktop\DemoDynamicObject\DemoDynamicObject\OrderBySpecialCharacter.cs 154 Active
I don't know how to fix it! Please help me! Thanks
If your have IComparer like this code:
public class MrFourCompare: IComparer<String>
{
public int Compare(string x, string y)
{
return string.Compare(x, y);
}
}
according your referred link you most change this line:
sortedList = listSort.OrderBy(x=> x.GetReflectedPropertyValue("Sort"),new MrFourCompare<string>()).ToList();
to
sortedList = listSort.OrderBy(x=> x.GetReflectedPropertyValue("Sort"),new MrFourCompare()).ToList();
because in implementation of IComparer you set it up to string.
Edit:
IComparer can not be Dynamic, so if you want to sort by Sort property of dynamic value you most cast sort property to exact type like String or Int.
sortedList = listSort.OrderBy(x=> (string)x.GetReflectedPropertyValue("Sort"),new MrFourCompare()).ToList();
Related
I have a use case where I need to check if a value is a C# 7 ValueTuple, and if so, loop through each of the items. I tried checking with obj is ValueTuple and obj is (object, object) but both of those return false. I found that I could use obj.GetType().Name and check if it starts with "ValueTuple" but that seems lame to me. Any alternatives would be welcomed.
I also have the issue of getting each item. I attempted to get Item1 with the solution found here: How do I check if a property exists on a dynamic anonymous type in c#? but ((dynamic)obj).GetType().GetProperty("Item1") returns null. My hope was that I could then do a while to get each item. But this does not work. How can I get each item?
Update - more code
if (item is ValueTuple) //this does not work, but I can do a GetType and check the name
{
object tupleValue;
int nth = 1;
while ((tupleValue = ((dynamic)item).GetType().GetProperty($"Item{nth}")) != null && //this does not work
nth <= 8)
{
nth++;
//Do stuff
}
}
Structures do not inherit in C#, so ValueTuple<T1>, ValueTuple<T1,T2>, ValueTuple<T1,T2,T3> and so on are distinct types that do not inherit from ValueTuple as their base. Hence, obj is ValueTuple check fails.
If you are looking for ValueTuple with arbitrary type arguments, you can check if the class is ValueTuple<,...,> as follows:
private static readonly Set<Type> ValTupleTypes = new HashSet<Type>(
new Type[] { typeof(ValueTuple<>), typeof(ValueTuple<,>),
typeof(ValueTuple<,,>), typeof(ValueTuple<,,,>),
typeof(ValueTuple<,,,,>), typeof(ValueTuple<,,,,,>),
typeof(ValueTuple<,,,,,,>), typeof(ValueTuple<,,,,,,,>)
}
);
static bool IsValueTuple2(object obj) {
var type = obj.GetType();
return type.IsGenericType
&& ValTupleTypes.Contains(type.GetGenericTypeDefinition());
}
To get sub-items based on the type you could use an approach that is not particularly fast, but should do the trick:
static readonly IDictionary<Type,Func<object,object[]>> GetItems = new Dictionary<Type,Func<object,object[]>> {
[typeof(ValueTuple<>)] = o => new object[] {((dynamic)o).Item1}
, [typeof(ValueTuple<,>)] = o => new object[] {((dynamic)o).Item1, ((dynamic)o).Item2}
, [typeof(ValueTuple<,,>)] = o => new object[] {((dynamic)o).Item1, ((dynamic)o).Item2, ((dynamic)o).Item3}
, ...
};
This would let you do this:
object[] items = null;
var type = obj.GetType();
if (type.IsGeneric && GetItems.TryGetValue(type.GetGenericTypeDefinition(), out var itemGetter)) {
items = itemGetter(obj);
}
Regarding the part of the question "How can I get each item?"...
Both ValueTuple and Tuple both implement ITuple, which has a length property and an indexer property. So a the following console app code lists the values to the console:
// SUT (as a local function)
IEnumerable<object> GetValuesFromTuple(System.Runtime.CompilerServices.ITuple tuple)
{
for (var i = 0; i < tuple.Length; i++)
yield return tuple[i];
}
// arrange
var valueTuple = (StringProp: "abc", IntProp: 123, BoolProp: false, GuidProp: Guid.Empty);
// act
var values = GetValuesFromTuple(valueTuple);
// assert (to console)
Console.WriteLine($"Values = '{values.Count()}'");
foreach (var value in values)
{
Console.WriteLine($"Value = '{value}'");
}
Console output:
Values = '4'
Value = 'abc'
Value = '123'
Value = 'False'
Value = '00000000-0000-0000-0000-000000000000'
This is my solution to the problem. A PCL compatible extension class. Special thanks to #dasblinkenlight and #Evk for helping me out!
public static class TupleExtensions
{
private static readonly HashSet<Type> ValueTupleTypes = new HashSet<Type>(new Type[]
{
typeof(ValueTuple<>),
typeof(ValueTuple<,>),
typeof(ValueTuple<,,>),
typeof(ValueTuple<,,,>),
typeof(ValueTuple<,,,,>),
typeof(ValueTuple<,,,,,>),
typeof(ValueTuple<,,,,,,>),
typeof(ValueTuple<,,,,,,,>)
});
public static bool IsValueTuple(this object obj) => IsValueTupleType(obj.GetType());
public static bool IsValueTupleType(this Type type)
{
return type.GetTypeInfo().IsGenericType && ValueTupleTypes.Contains(type.GetGenericTypeDefinition());
}
public static List<object> GetValueTupleItemObjects(this object tuple) => GetValueTupleItemFields(tuple.GetType()).Select(f => f.GetValue(tuple)).ToList();
public static List<Type> GetValueTupleItemTypes(this Type tupleType) => GetValueTupleItemFields(tupleType).Select(f => f.FieldType).ToList();
public static List<FieldInfo> GetValueTupleItemFields(this Type tupleType)
{
var items = new List<FieldInfo>();
FieldInfo field;
int nth = 1;
while ((field = tupleType.GetRuntimeField($"Item{nth}")) != null)
{
nth++;
items.Add(field);
}
return items;
}
}
hackish one liner
type.Name.StartsWith("ValueTuple`")
(can be extended to check the digit at the end)
I have to cast enum but I want this to be as generic as possbile. How can I replace the Cast<XX>() part with propertyType?
foreach (var prop in MainType.GetProperties().Where(x => x.PropertyType.IsEnum))
{
var x = new { name = prop.Name, values = new List<object>() };
foreach (var v in Enum.GetValues(prop.PropertyType).Cast<XX>())
x.values.Add(new { p = v.GetAttribute<DescriptionAttribute>().Description,
c = v.GetAttribute<DefaultValueAttribute>().Value });
o.Add(x);
}
public static TAttribute GetAttribute<TAttribute>(this Enum value) where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
public enum JobApplicationState : short
{
[Description("Active")]
[DefaultValue(typeof(string), "bg-primary text-highlight")]
Active = 1,
[Description("Passive")]
[DefaultValue(typeof(string), "bg-grey text-highlight")]
Passive = 2,
[Description("Rejected")]
[DefaultValue(typeof(string), "bg-danger text-highlight")]
Rejected = 3,
[Description("Accepted")]
[DefaultValue(typeof(string), "bg-success text-highlight")]
Accepted = 4
}
THIS WORKED!
foreach (MemberInfo m in prop.PropertyType.GetFields())
{
if (m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() != null && m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault() != null)
{
x.values.Add(
new
{
p = ((DescriptionAttribute)m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description,
c = ((DefaultValueAttribute)m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault()).Value
});
}
}
You should realize that an enum is nothing more than a named wrapper around an integral value. I've described some details on how enums work here: Cast int to enum in C# .
The question here is to get attributes values from an enum. As you might imagine, this question is about getting attributes from the type, not about getting attributes from the value (there's no such thing). Still, if you call a method like void Foo<T>(T myEnum), the type T will hold all the necessary info, even though 'in real life' the value of myEnum is passed around as an integral type.
From this you can also deduce that you're actually looking for the attributes of the MemberInfo's of T, since you're looking for the members of the type (e.g. the enum). Again, all the details are in my post on how enums work. Hence the answer:
foreach (MemberInfo m in prop.PropertyType.GetFields())
{
// use m.GetAttribute(...)
}
Why donĀ“t you simply cast to Enum:
foreach (var v in Enum.GetValues(prop.PropertyType).Cast<Enum>())
Now you can call your extension-method on every element within the returned list.
v.GetAttribute<MyType>()
I am facing problem in my code, its telling system.int32 cant not be converted to system.double, here is my code
I have an object with 4 string variables and 2 integer variables.
public class MyObject
{
public int id;
public string name1;
public string name2;
public string name3;
public string name4;
public int id2;
}
/*Converts DataTable To List*/
public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
{
var dataList = new List<TSource>();
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
select new { Name = aProp.Name, Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType }).ToList();
var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList();
var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();
foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
{
var aTSource = new TSource();
foreach (var aField in objFieldNames)
{
PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
propertyInfos.SetValue(aTSource, dataRow[aField.Name], null);
}
dataList.Add(aTSource);
}
return dataList;
}
In my objectfieldname variable i am getting all the variable with right data type, but in dataTblFieldNames variable its converting int to double, so i am facing problem in propertyinfos in foreach loop its saying
Object of type 'System.Double' cannot be converted to type 'System.Int32'.?
Any one help me to fix this please?
You can use Convert.ChangeType method:
Convert.ChangeType(dataRow[aField.Name], propertyInfos.PropertyType)
It will try some numeric conversions (like your conversion from double to int, a change in binary representation from floating point to integer (2's complement)) and some formatting/parsing to/from string.
You have find property type before assigning value and you have to also cast value to the type required because you can not directly assign double type to int type.
if (propertyInfos.PropertyType == typeof(int))
{
propertyInfos.SetValue(aTSource, Convert.ToInt32(dataRow[aField.Name]), null);
}
Your input dataRow[aField.Name] is of type System.Double while the field you want to assign to is of type System.Int32. But Visual Studio already told you so loud and clear.
I cannot tell you what to do because I don't know what you want to do. Truncate the double by casting? Round it up or down? Maybe it's the wrong field altogether? we don't know. You decide.
Considering the fact that it's pretty unlikely you came up with the above code and still did not understand the error, maybe it's a good idea to either get a full nights sleep and a good coffee or ask the one who originally wrote this code.
Perhaps the question title is incorrect. I have the following variables
IEnumerable x = // some IEnumerable
System.Type y = // some type
How can iterate over x in order to generate an array with items of type y?
When I look into the internet I found:
public T[] PerformQuery<T>(IEnumerable q)
{
T[] array = q.Cast<T>().ToArray();
return array;
}
Note I cannot call that method PerformQuery becuase y is of type System.Type in other words calling it as PerformQuery<typeof(y)>(x); or PerformQuery<y>(x); will give me a compiler error.
edit
Here is the reason why I have that problem. I have web service where I post to it two things. The type of table I will like to query (example typeof(Customer)), and the actual string query example "Select * from customers"
protected void Page_Load(object sender, EventArgs e)
{
// code to deserialize posted data
Type table = // implement that here
String query = // the query that was posted
// note DB is of type DbContext
IEnumerable q = Db.Database.SqlQuery(table, query );
// here I will like to cast q to an array of items of type table!
You can use Expression Trees:
public static class MyExtensions
{
public static Array ToArray(this IEnumerable source, Type type)
{
var param = Expression.Parameter(typeof(IEnumerable), "source");
var cast = Expression.Call(typeof(Enumerable), "Cast", new[] { type }, param);
var toArray = Expression.Call(typeof(Enumerable), "ToArray", new[] { type }, cast);
var lambda = Expression.Lambda<Func<IEnumerable, Array>>(toArray, param).Compile();
return lambda(source);
}
}
It generates x => x.Cast<Type>().ToArray() for you, with Type known at runtime.
Usage:
IEnumerable input = Enumerable.Repeat("test", 10);
Type type = typeof(string);
Array result = input.ToArray(type);
var ObjectsOfType_y = x.OfType<object>().Where(x => x.GetType() == y);
Notice that this will return an IEnumerable<object>, though. There's no way around that because the type that y (Type) represents is unknown at compile time.
According to my understanding, IENumerable contains only one type. If I understand what you're trying to do, IENumerable already only contains only objects of type y. If y needs to change, you can write an extension method:
public static T[] ToArray<T>(this IEnumerable<T> source)
{
int length = System.Linq.Enumerable.Count(source);
T[] newArray = new T[length];
int i = 0;
foreach(T item in source)
{
newArray[i] = item;
}
return newArray;
}
I have a C# method that I need to pass various lists into. The type of lists will be different very often. The method itself is in a different dll and cannot know the class objects contained in the lists (no references, no using statements). The method needs to read each of the members of each of the items in a list and build and return a string based off of that.
My question is how can get the metadata for for these various objects at compiletime / runtime? Will reflection do that?
Also, once I get the metadata, how do i use it to actually use the variables?
Thank you
EDIT: currently I am using it like the following:
public string GetData(List<User> list){
//...
List<RowData> rows = new List<RowData>();
foreach (User item in list)
{
RowData row = new RowData();
row.id = count++;
row.cell = new string[3];
row.cell[0] = item.ID.ToString();
row.cell[1] = item.name;
row.cell[2] = item.age.ToString();
rows.Add(row);
}
//...
return new JavaScriptSerializer().Serialize(rows.ToArray());
Right now this is very specific. I want to replace this with generics so I can pass items other than "User"
If the parameter (will call it list for discussion's sake) coming into the method is List<T> then you can do the following:
Type type = list.GetType().GetGenericArguments()[0];
If you just have a generic T class, then you can always just go with this:
Type typeParameterType = typeof(T);
.GetType() will get you a Type and you can get a lot of descriptions about the type.
You can also discover members of the instances you have using reflection
Please be precise with what you want more exactly ?
EDIT : Here is a way, try making it an extenstion method it would be better
public static List<RowData> ToDataTable<T>(IEnumerable<T> source)
{
System.Reflection.PropertyInfo[] properties = typeof(T).GetProperties();
List<RowData> rows = new List<JQGridRow>();
foreach (var item in source)
{
RowData row = new RowData();
row.cells = new string[properties.Length];
int i=0;
foreach (var prop in properties)
{
row.cells[i] = prop.GetValue(item, null);i++;
}
rows.Add(row);
}
return rows;
}
Yes, you can use reflection to obtain the type of an object at runtime.
Simple call to object.GetType() will give you the type of this instance.
What you do with this information is up to you.
Maybe something like this?
public string GetData<T>(IEnumerable<T> list, Func<T, RowData> fillRow)
{
List<RowData> rows = new List<JQGridRow>();
foreach (User item in list)
{
RowData row = new RowData();
row.id = count++;
fillRow(row);
rows.Add(row);
}
//...
return new JavaScriptSerializer().Serialize(rows.ToArray());
}
And use it like
string s = GetData(users, row =>
{
row.cell = new string[3];
row.cell[0] = item.ID.ToString();
row.cell[1] = item.name;
row.cell[2] = item.age.ToString();
});
This might work if you treat them all as objects, using the intrinsic ToString() method for your string building.