I am trying to create an instance of an anonymous type:
new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() }
In .NET, the darkness creates an anonymous type with a constructor that takes three arguments: String, String, Int32.
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 unloads me:
Additional Information: Constructor not found in '<> f__AnonymousType2`3 [[System.String, ...], [System.String, ...], [System.Int32, ...]]'.
I donโt know why CreateInstancetrying to call a constructor like [[], [], []]!
Region
The actual volume is a little harder to explain:
I created a Linq provider. This provider translates Linq suggestions to my server methods. When I get json information, I need to project this information depending on which Type user 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() }). .
:
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);
}
yield return (T)Activator.CreateInstance(typeof(T), args);
}
}