C#'dynamic'无法访问在另一个程序集中声明的匿名类型的属性 [英] C# ‘dynamic’ cannot access properties from anonymous types declared in another assembly

查看:748
本文介绍了C#'dynamic'无法访问在另一个程序集中声明的匿名类型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下只要我有类 ClassSameAssembly 在相同的组件,类程序运作良好。
,而当我移动类 ClassSameAssembly 到一个单独的组件, RuntimeBinderException (见下文)被抛出。
是否可以解决它?

 使用System; 

命名空间ConsoleApplication2
{
公共静态类ClassSameAssembly
{
公共静态动态的GetValues()
{
返回新的
{
Name =Michael,Age = 20
};
}
}

内部类节目
{
私有静态无效的主要(字串[] args)
{
变种d = ClassSameAssembly.GetValues();
Console.WriteLine({0} is {1} years old,d.Name,d.Age);
}
}
}

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException 对象不包含名称



<$ p $的定义p> 在在System.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0,TRET](调用点现场,T0为arg0)
在ConsoleApplication2 CallSite.Target(封闭,调用点,对象)
。 Program.Main(字串[] args)在C:\temp\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:23行


解决方案

我相信问题是匿名类型为产生的内部,所以



尝试使用ExpandoObject:

  public static dynamic GetValues()
{
dynamic expando = new ExpandoObject();
expando.Name =Michael;
expando.Age = 20;
return expando;
}



我知道这有点丑陋,但它是我能想到的最好的时间...我不认为你甚至可以使用对象初始化程序,因为它是强类型 ExpandoObject 编译器不知道该怎么做名称和年龄。 可以:

 动态expando = new ExpandoObject {
{Name,Michael},
{Age,20}
};
return expando;

但这不是很好...



您可以潜在地编写一个扩展方法,通过反射将匿名类型转换为具有相同内容的expando。然后你可以写:

  return new {Name =Michael,Age = 20} .ToExpando 

这是非常可怕的:(


Code below is working well as long as I have class ClassSameAssembly in same assembly as class Program. But when I move class ClassSameAssembly to a separate assembly, a RuntimeBinderException (see below) is thrown. Is it possible to resolve it?

using System;

namespace ConsoleApplication2
{
    public static class ClassSameAssembly
    {
        public static dynamic GetValues()
        {
            return new
            {
                Name = "Michael", Age = 20
            };
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var d = ClassSameAssembly.GetValues();
            Console.WriteLine("{0} is {1} years old", d.Name, d.Age);
        }
    }
}

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'

at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at ConsoleApplication2.Program.Main(String[] args) in C:\temp\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 23

解决方案

I believe the problem is that the anonymous type is generated as internal, so the binder doesn't really "know" about it as such.

Try using ExpandoObject instead:

public static dynamic GetValues()
{
    dynamic expando = new ExpandoObject();
    expando.Name = "Michael";
    expando.Age = 20;
    return expando;
}

I know that's somewhat ugly, but it's the best I can think of at the moment... I don't think you can even use an object initializer with it, because while it's strongly typed as ExpandoObject the compiler won't know what to do with "Name" and "Age". You may be able to do this:

 dynamic expando = new ExpandoObject()
 {
     { "Name", "Michael" },
     { "Age", 20 }
 };
 return expando;

but that's not much better...

You could potentially write an extension method to convert an anonymous type to an expando with the same contents via reflection. Then you could write:

return new { Name = "Michael", Age = 20 }.ToExpando();

That's pretty horrible though :(

这篇关于C#'dynamic'无法访问在另一个程序集中声明的匿名类型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆