简体   繁体   English

跨 dll 边界访问匿名/动态类型的属性会导致 RuntimeBinderException

[英]Accessing properties of anonymous/dynamic types across dll boundaries gives RuntimeBinderException

In the following sample, x.propertyX works fine, whereas y.propertyX gives me a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException , complaining 'propertyX' is not defined in 'object'.在下面的示例中, x.propertyX工作正常,而y.propertyX给了我一个Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ,抱怨“object”中没有定义“propertyX”。

The CreateDynamic method in the Program class (shown below) and the one in Class1 (not shown) are exactly the same, but Class1 is in a different project from Program. Program 类中的 CreateDynamic 方法(如下所示)和 Class1 中的 CreateDynamic 方法(未显示)完全相同,但 Class1 与 Program 在不同的项目中。 If I move Class1 into Program's project, everything works fine.如果我将 Class1 移到 Program 的项目中,则一切正常。

class Program
{
    public static object CreateDynamic()
    {
        return new { propertyX = "asdf" };
    }

    static void Main(string[] args)
    {
        dynamic x = CreateDynamic();
        Console.WriteLine(x.propertyX);

        dynamic y = Class1.CreateDynamic();
        Console.WriteLine(y.propertyX);

What do I need to do to make anonymous types work across dlls as dynamic types - or is that not possible?我需要做什么才能使匿名类型作为动态类型跨 dll 工作 - 或者这是不可能的?

Update: Fwiw, I figured out that I can get around that using ExpandoObjects, which I then 'cast' to dynamic, but ExpandoObjects are are not as nicely instantiable, when compared to the更新: Fwiw,我发现我可以使用 ExpandoObjects 解决这个问题,然后我将其“转换”为动态,但与

new { key1 = val1, key2 = val2 }

style that anonymous types offer.匿名类型提供的样式。

Anonymous types are internal to the assembly they are created in. If you have control over the source code you can make them Friend Assemblies匿名类型是创建它们的程序集内部的。如果您可以控制源代码,您可以将它们设为Friend 程序集

[assembly:InternalsVisibleTo("TheOtherAssembly")]

but there are drawbacks .但也有缺点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在视图中访问动态匿名类型时的RuntimeBinderException - RuntimeBinderException when accessing dynamic anonymous type in view 将JSON对象反序列化为动态类型,但获取RuntimeBinderException访问属性? - Deserialized JSON object to dynamic type but getting RuntimeBinderException accessing properties? RuntimeBinderException 与 MVC 中的动态匿名对象 - RuntimeBinderException with dynamic anonymous objects in MVC 跨程序集边界返回/使用动态匿名类型 - Return/consume dynamic anonymous type across assembly boundaries 在C#中访问匿名类型的属性? - Accessing properties of an anonymous types in C#? 从动态类型中抑制RuntimeBinderException消息 - Suppress RuntimeBinderException messages from dynamic types 跨AppDomain边界代理匿名对象 - Proxying Anonymous Objects across AppDomain boundaries 如何使用“动态”变量从匿名类型中读取属性 - How to read properties from anonymous types using “dynamic” variable 在私有类型上动态抛出Microsoft.CSharp.RuntimeBinder.RuntimeBinderException - Dynamic throwing Microsoft.CSharp.RuntimeBinder.RuntimeBinderException on private types 在C#中调用动态PSObject属性将返回RuntimeBinderException - Calling dynamic PSObject Properties in C# returns RuntimeBinderException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM