解决方案 »
- 如何将div或者table种的数据用图片保存出
- picturebox绘图的问题!
- 这问题我两天都没搞定,大家帮我看看
- WLT文件转换为BMP或者JPG文件?
- 请教一个在Winform连接数据库有关数据类型的问题
- c#我要实现在服务器里监控人家上传的文件,对.exe /mp3/avi等格式进行报警(邮件),高手赐教
- 在C#中如何得到 MSComm 的Input 属性的值?急呀?
- dataGrid1所显示的内容怎么保存为txt文件或excl文件?
- C#做的安装程序在安装的时候如何将一个文件安装到system32下,win2000可不一定安装到C盘啊
- C转C#代码 多边形裁切Clip gpc
- c#中的一个设计问题请教?
- 没法加载 DLL “SQLite.Interop.DLL”
我已经利用反射,获取到body部分了,即(body×3)45 67 89
也知道了body部分的Typeif (property.PropertyType.IsGenericType)
{
if (property.PropertyType.GenericTypeArguments.Length > 0)
{
Type childType = property.PropertyType.GenericTypeArguments[0];
PropertyInfo[] childPropertyInfo = childType.GetProperties();
//how to generate a list<childType> ?
}
}那么如何给body赋值呢?
继续处理(body×3)45 67 89呀
继续处理(body×3)45 67 89呀
不知道如何给List<T>赋值啊,我写了一个方法,如下,private List<object> GenerateList(Type type, string codes)
{
List<PropertyInfo> propertyInfoList = type.GetProperties().OrderBy(item =>
item.GetCustomAttribute<BytesAttribute>().Order
).ToList(); int itemLength = 0;
propertyInfoList.ForEach(property => itemLength += property.GetCustomAttribute<BytesAttribute>().Length);
int itemCount = codes.Length / itemLength;
string itemCodes; List<object> temp = new List<object>(); for (int i = 0; i < itemCount; i++)
{
itemCodes = codes.Substring(i * itemLength, itemLength);
object childObj = (object)Activator.CreateInstance(type, itemCodes); temp.Add(childObj);
} return temp;
}返回的是一个List<object>类型的,但是我需要的是List<Class8_1>类型的,当使用
property.SetValue(this, GenerateList(childType, value));
这句时,会报异常:类型“System.Collections.Generic.List`1[System.Object]”的对象无法转换为类型“System.Collections.Generic.List`1[JsonTest.Class8_1]”。即类型不对,那我如何强制给它转换过来呢?目前手头只有type
困扰啊。
然后用那方法替换object childObj = (object)Activator.CreateInstance(type, itemCodes);
这么写可以不?
if (property.PropertyType.IsGenericType) //假如该属性为泛型类型
{
Type t = property.PropertyType.GetGenericTypeDefinition(); if (t == typeof(List<>))
{
Type childType = property.PropertyType.GenericTypeArguments[0];
List<object> objList = GenerateList(childType, value); //出错:类型“System.Collections.Generic.List`1[System.Object]”的对象
//无法转换为类型“System.Collections.Generic.List`1[JsonTest.Class8_1]”。
property.SetValue(this, objList);
}
}
{
List<PropertyInfo> propertyInfoList = type.GetProperties().OrderBy(item =>
item.GetCustomAttribute<BytesAttribute>().Order
).ToList();
int itemLength = 0;
propertyInfoList.ForEach(property => itemLength += property.GetCustomAttribute<BytesAttribute>().Length);
int itemCount = codes.Length / itemLength;
string itemCodes;
List<T> temp = new List<T>();
for (int i = 0; i < itemCount; i++)
{
itemCodes = codes.Substring(i * itemLength, itemLength);
T childObj = (T)Activator.CreateInstance(type, itemCodes);
temp.Add(childObj);
}
return temp;
}
这么写可以不?
很感谢你帮我写啊,我使用泛型方法如下,private List<T> GenerateList<T>(T obj, string codes)
{
List<PropertyInfo> propertyInfoList = obj.GetType().GetProperties().OrderBy(item =>
item.GetCustomAttribute<BytesAttribute>().Order
).ToList(); int itemLength = 0;
propertyInfoList.ForEach(property => itemLength += property.GetCustomAttribute<BytesAttribute>().Length);
int itemCount = codes.Length / itemLength;
string itemCodes; List<T> objList = new List<T>(); for (int i = 0; i < itemCount; i++)
{
itemCodes = codes.Substring(i * itemLength, itemLength);
T childObj = (T)Activator.CreateInstance(obj.GetType(), itemCodes); objList.Add(childObj);
} return objList;
}
然后就不知道如何调用了。= = 大哭if (property.PropertyType.IsGenericType) //假如该属性为泛型类型
{
Type t = property.PropertyType.GetGenericTypeDefinition(); if (t == typeof(List<>))
{
Type childType = property.PropertyType.GenericTypeArguments[0]; //如何依据Type产生一个class8_1的实例,作为参数传入GenerateList(Class8_1 obj, string value) //就像这样:
//Class8_1 test = new Class8_1(string.Empty);
//List<Class8_1> objList = GenerateList(test, value); property.SetValue(this, GenerateList(t1, value));
}
}
private List<T> GenerateList<T>(string codes)
{
通过 T获得属性列表;
}
调用的地方
childType后面
var callExpr = Expression.Call(Expression.Constant(this), "GenerateList", new Type[] { childType }, Expression.Constant(value));
var list = Expression.Lambda<Func<object>>(callExpr).Compile()();
property.SetValue(this,list);