使用反射初始化嵌套复杂类型

发布于 2024-12-06 04:51:05 字数 602 浏览 2 评论 0原文

代码树就像:

Class Data
{
    List<Primitive> obj;
}

Class A: Primitive
{
    ComplexType CTA;
}

Class B: A
{
    ComplexType CTB;
    Z o;
}

Class Z
{
   ComplexType CTZ;
}

Class ComplexType { .... }

现在在 List中obj 中,有许多类的 ComplexType 对象为“null”。我只是想将其初始化为某个值。

问题是如何使用反射来遍历完整的树。

编辑:

Data data = GetData(); //All members of type ComplexType are null. 
ComplexType complexType = GetComplexType();

我需要将“data”中的所有“ComplexType”成员初始化为“complexType”

Code tree is like:

Class Data
{
    List<Primitive> obj;
}

Class A: Primitive
{
    ComplexType CTA;
}

Class B: A
{
    ComplexType CTB;
    Z o;
}

Class Z
{
   ComplexType CTZ;
}

Class ComplexType { .... }

Now in List<Primitive> obj, there are many classes in which ComplexType object is 'null'. I just want to initialize this to some value.

The problem is how to traverse the complete tree using reflection.

Edit:

Data data = GetData(); //All members of type ComplexType are null. 
ComplexType complexType = GetComplexType();

I need to initialize all 'ComplexType' members in 'data' to 'complexType'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野稚 2024-12-13 04:51:05

如果我理解正确的话,也许像这样的东西会起作用:

static void AssignAllComplexTypeMembers(object instance, ComplexType value)
{
     // If instance itself is null it has no members to which we can assign a value
     if (instance != null)
     {
        // Get all fields that are non-static in the instance provided...
        FieldInfo[] fields = instance.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

        foreach (FieldInfo field in fields)
        {
           if (field.FieldType == typeof(ComplexType))
           {
              // If field is of type ComplexType we assign the provided value to it.
              field.SetValue(instance, value);
           }
           else if (field.FieldType.IsClass)
           {
              // Otherwise, if the type of the field is a class recursively do this assignment 
              // on the instance contained in that field. (If null this method will perform no action on it)
              AssignAllComplexTypeMembers(field.GetValue(instance), value);
           }
        }
     }
  }

并且这个方法将被称为:

foreach (var instance in data.obj)
        AssignAllComplexTypeMembers(instance, t);

这个代码当然只适用于字段。如果您还需要属性,则必须让循环遍历所有属性(可以通过 instance.GetType().GetProperties(...) 检索)。

但请注意,这种反射并不是特别有效。

If I understand you correctly perhaps something like this would do the trick:

static void AssignAllComplexTypeMembers(object instance, ComplexType value)
{
     // If instance itself is null it has no members to which we can assign a value
     if (instance != null)
     {
        // Get all fields that are non-static in the instance provided...
        FieldInfo[] fields = instance.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

        foreach (FieldInfo field in fields)
        {
           if (field.FieldType == typeof(ComplexType))
           {
              // If field is of type ComplexType we assign the provided value to it.
              field.SetValue(instance, value);
           }
           else if (field.FieldType.IsClass)
           {
              // Otherwise, if the type of the field is a class recursively do this assignment 
              // on the instance contained in that field. (If null this method will perform no action on it)
              AssignAllComplexTypeMembers(field.GetValue(instance), value);
           }
        }
     }
  }

And this method would be called like:

foreach (var instance in data.obj)
        AssignAllComplexTypeMembers(instance, t);

This code only works for fields of course. If you want properties as well you would have to have the loop iterate through all properties (which can be retreived by instance.GetType().GetProperties(...)).

Be aware though that reflection is not particularly efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文