本帖最后由 TongRay 于 2013-07-07 20:08:24 编辑

解决方案 »

  1.   

    如果程序中有这个类的类型,可以不需要反射。直接new。
      

  2.   

    嗯,是这样的,我有很多大类数据都是用string来传递的,它们的基本结构都很类似(都有一个信息栏位,多个详情栏位,还有个结尾的校验栏位),只是每个大类下得栏位长度可能不同,不想给每个大类都写一堆代码(估计是6分类50多个类),所以想问问明白。
      

  3.   

    我是想从字符串content生成Class8的一个实例。
      

  4.   

    反射一般是适用于从dll生成类的
      

  5.   

    目前进度如下,
    我已经利用反射,获取到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赋值呢?
      

  6.   


    继续处理(body×3)45 67 89呀
      

  7.   


    继续处理(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
      

  8.   

    目前我知道了List<T>中存的类型是Type childType(即Class8_1),我如何把List<object>转为List<Class8_1>?
    困扰啊。
      

  9.   

    自己实现从string到Class8_1的转换
    然后用那方法替换object childObj = (object)Activator.CreateInstance(type, itemCodes);
      

  10.   

    private List<T> GenerateList(Type type, string codes)
    这么写可以不?
      

  11.   


    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); 
    }
    }
      

  12.   

    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;
    }
      

  13.   

    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));
    }
    }
      

  14.   


    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);