本文介绍了ExpandoObject对象和GetProperty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过外部.NET(/ skip long story)的COM编写一个通用的实用程序。无论如何,我试图添加一个ExpandoObject的属性,我需要获取PropertyInfo结构返回到另一个例程。

I'm trying to write a generic utility for use via COM from outside .NET (/skip long story). Anyway, I'm trying to add properties to an ExpandoObject and I need to get PropertyInfo structure back to pass to another routine.

using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Reflection;

public class ExpandoTest
{
    public string testThis(string cVariable)
    {
        string cOut = "";

        ExpandoObject oRec = new ExpandoObject { };
        IDictionary<string, object> oDict = (IDictionary<string, object>)oRec;

        oDict.Add(cVariable, "Test");

        Trace.WriteLine(cVariable);
        Trace.WriteLine(oDict[cVariable]);

        PropertyInfo thisProp = oRec.GetType().GetProperty(cVariable);

        if (thisProp != null)
        {
            cOut= "Got a property :)";
        }

        return cOut;
    }
}

为什么在thisProp中总是得到一个null?我显然不明白,但我一直盯着它一天,我没有得到任何地方。所有的帮助/批评感谢您的接受!

Why do I always get a null in in thisProp? I clearly don't understand but I've been staring at it for a day and I'm not getting anywhere. All help/criticism thankfully accepted!

推荐答案

使用 ExpandoObject 可能看起来你可以在运行时添加属性,它实际上不会在CLR级别上这样做。这就是为什么使用反射来获取在运行时添加的属性将不起作用。

While using an ExpandoObject it might look like you can add properties at runtime, it won't actually do that at the CLR level. That's why using reflection to get the property you added at runtime won't work.

它有助于想到一个 ExpandoObject 作为字典将字符串映射到对象。当您将 ExpandoObject 视为动态变量时,任何对属性的调用都将路由到该字典。

It helps to think of an ExpandoObject as a dictionary mapping strings to objects. When you treat an ExpandoObject as a dynamic variable any invocation of a property gets routed to that dictionary.

dynamic exp = new ExpandoObject();
exp.A = "123";

实际调用相当复杂,涉及到DLR,但其效果与写入



The actual invocation is quite complex and involves the DLR, but its effect is the same as writing

((IDictionary<string, object>)exp)["A"] = "123";

这也只能在使用动态时有效。上面的代码的强类型版本导致编译时错误。

This also only works when using dynamic. A strongly typed version of the code above results in a compile-time error.

var exp = new ExpandoObject();
exp.A = "123"; // compile-time error






实际执行 ExpandoObject 可以在。

这篇关于ExpandoObject对象和GetProperty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 13:43