创建一个新的AnonymousType实例 [英] Create a new AnonymousType instance

查看:112
本文介绍了创建一个新的AnonymousType实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个AnonymousType实例,如下所示:

I'm trying to create an AnonymousType instance looks like:

new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() }

在黑暗中,.NET使用带有三个参数的构造函数创建一个AnonymousType:String, String, Int32.

On the dark, .NET creates an AnonymousType with a constructor that takes three arguments: String, String, Int32.

为了创建这种匿名类型T的新实例,我要做:

In order to create a new instance of this anonymous type, T, I do:

object[] args = new object[3];
args[0] = "arg1";
args[1] = "arg2";
args[2] = 200;
(T)Activator.CreateInstance(typeof(T), args);

.NET转储了我

其他信息:在'<> f__AnonymousType2`3 [[System.String,...],[System.String,...],[System.Int32,...]]'中找不到构造函数.

Additional information: Constructor not found in '<>f__AnonymousType2`3[[System.String, ...],[System.String, ...],[System.Int32, ...]]'.

我不知道为什么CreateInstance试图调用类似[[],[],[]]的构造函数!

I don't know why CreateInstance is trying to call a constructor like [[],[],[]]!

范围

真正的范围很难解释:

我创建了一个Linq提供程序.该提供程序将Linq句子翻译为我的服务器方法.当我收到json信息时,我需要将此信息投影到指定的Type用户.在这种情况下:

I've created a Linq provider. This provider translates Linq sentences to my server methods. When I receive json information I need to project this information to whichever Type user has specified. In this case:

var enumerable = UIContainer.UIController.Instance.getDigitalInputs()
    .GroupBy(di => new { Channel = di.Channel, Comment = di.Comment })
    .Select(g => new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() });

因此,我需要将每个json项目投影到new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() }).最后,我需要创建这种匿名类型的实例.

So, I need to project each json item to a new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() }). At the end I need to create an instance of this anonymous type.

所以:

// make the HTTP request
IRestResponse response = (IRestResponse) this.client.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams, authSettings);

if (((int)response.StatusCode) >= 400) {
    throw new ApiException (response.StatusCode, "Error calling Search: " + response.Content, response.Content);
}

Newtonsoft.Json.Linq.JArray feeds = Newtonsoft.Json.Linq.JArray.Parse(response.Content);
if (feeds.Any())
{
     PropertyDescriptorCollection dynamicProperties = TypeDescriptor.GetProperties(feeds.First());
     foreach (dynamic feed in feeds)
     {
         object[] args = new object[dynamicProperties.Count];
         int i = 0;
         foreach (PropertyDescriptor prop in dynamicProperties)
         {
             args[i++] = prop.GetValue(feed);
         }
         //args[0] = "";
         //args[1] = "";
         //args[2] = 2;

         yield return (T)Activator.CreateInstance(typeof(T), args);
    }
}

推荐答案

不确定从哪里获取T,但是如果您使用前一个变量的匿名类型,则代码可以正常工作:

Not sure where you obtain T, but the code works fine if you use the anonymous type from the previous variable:

var x = new { Channel = "Channel", Comment = "Comment", Count = 1 };

object[] args = new object[3];
args[0] = "arg1";
args[1] = "arg2";
args[2] = 200;
var y = Activator.CreateInstance(x.GetType(), args);

(然后回复Luaan:.NET使用匿名类型的构造函数,请参见IL:)

(And in to reply to Luaan: .NET uses a constructor for anonymous types, see the IL:)

.method public hidebysig specialname rtspecialname 
        instance void  .ctor(!'<Channel>j__TPar' Channel,
                             !'<Comment>j__TPar' Comment,
                             !'<Count>j__TPar' Count) cil managed

这篇关于创建一个新的AnonymousType实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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