如何从字符串生成类?反序列化还是反射得到呢?
--------------------编程问答-------------------- 如果程序中有这个类的类型,可以不需要反射。直接new。 --------------------编程问答-------------------- 楼主,示例中有两个类,您要生成的是哪个类? --------------------编程问答--------------------嗯,是这样的,我有很多大类数据都是用string来传递的,它们的基本结构都很类似(都有一个信息栏位,多个详情栏位,还有个结尾的校验栏位),只是每个大类下得栏位长度可能不同,不想给每个大类都写一堆代码(估计是6分类50多个类),所以想问问明白。 --------------------编程问答--------------------
我是想从字符串content生成Class8的一个实例。 --------------------编程问答-------------------- 反射一般是适用于从dll生成类的 --------------------编程问答-------------------- 我所知道的也不算太多,还是等版主来给您详细解答吧 --------------------编程问答-------------------- 目前进度如下,
我已经利用反射,获取到body部分了,即(body×3)45 67 89
也知道了body部分的Type
if (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呀 --------------------编程问答--------------------
不知道如何给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 --------------------编程问答-------------------- 目前我知道了List<T>中存的类型是Type childType(即Class8_1),我如何把List<object>转为List<Class8_1>?
困扰啊。 --------------------编程问答-------------------- 自己实现从string到Class8_1的转换
然后用那方法替换object childObj = (object)Activator.CreateInstance(type, itemCodes); --------------------编程问答--------------------
目前我知道了List<T>中存的类型是Type childType(即Class8_1),我如何把List<object>转为List<Class8_1>?
困扰啊。
private List<T> GenerateList(Type type, string codes)
这么写可以不? --------------------编程问答--------------------
--------------------编程问答--------------------
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);
}
}
private List<T> 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<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;
}
目前我知道了List<T>中存的类型是Type childType(即Class8_1),我如何把List<object>转为List<Class8_1>?
困扰啊。
private List<T> GenerateList(Type type, string codes)
这么写可以不?
很感谢你帮我写啊,我使用泛型方法如下,
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);
补充:.NET技术 , C#