如何在c#中获取动态对象的类型?

时间:2021-05-18 21:21:17

I have a List<dynamic> and I need to pass the values and type of the list to a service. The service code would be something like this:

我有一个列表 ,我需要将列表的值和类型传递给一个服务。服务代码是这样的:

 Type type = typeof(IList<>);
 // Type genericType = how to get type of list. Such as List<**string**>, List<**dynamic**>, List<**CustomClass**>
 // Then I convert data value of list to specified type.
 IList data = (IList)JsonConvert.DeserializeObject(this._dataSourceValues[i], genericType);

_dataSourceValues: values in the list

_dataSourceValues:列表中的值

How can I cast the type of the list to a particular type if the type of List is dynamic (List<dynamic>)?

如果列表的类型是动态的(list ),如何将列表的类型转换为特定类型?

2 个解决方案

#1


1  

If I understand you properly you have a List<dynamic> and you want to create a List with the appropriate runtime type of the dynamic object?

如果我理解正确,您有一个列表 ,您想要创建一个具有动态对象的适当运行时类型的列表?

Would something like this help:

像这样的东西会有用吗:

private void x(List<dynamic> dynamicList)
{
    Type typeInArgument = dynamicList.GetType().GenericTypeArguments[0];
    Type newGenericType = typeof(List<>).MakeGenericType(typeInArgument);
    IList data = (IList)JsonConvert.DeserializeObject(this._dataSourceValues[i], newGenericType);
}

On another note, I think you should rethink the design of your code. I don't really have enough context but I'm curious as to why you are using dynamic here. Having a List<dynamic> basically means you don't care what the type of the incoming list is. If you really care for the type(seems like you do as you are doing serialization) maybe you shouldn't be using dynamic.

另一方面,我认为您应该重新考虑代码的设计。我没有足够的背景知识,但我很好奇为什么你在这里使用动态。有一个列表 基本上意味着您不关心传入列表的类型。如果您真的喜欢这个类型(看起来就像您在进行序列化一样),那么您可能不应该使用dynamic。

#2


1  

 private void x(List<dynamic> dynamicList)
            {
                Type typeInArgument = dynamicList.GetType();
                Type newGenericType = typeof(List<>).MakeGenericType(typeInArgument);
                IList data = (IList)JsonConvert.DeserializeObject(this._dataSourceValues[i], newGenericType);
            }

#1


1  

If I understand you properly you have a List<dynamic> and you want to create a List with the appropriate runtime type of the dynamic object?

如果我理解正确,您有一个列表 ,您想要创建一个具有动态对象的适当运行时类型的列表?

Would something like this help:

像这样的东西会有用吗:

private void x(List<dynamic> dynamicList)
{
    Type typeInArgument = dynamicList.GetType().GenericTypeArguments[0];
    Type newGenericType = typeof(List<>).MakeGenericType(typeInArgument);
    IList data = (IList)JsonConvert.DeserializeObject(this._dataSourceValues[i], newGenericType);
}

On another note, I think you should rethink the design of your code. I don't really have enough context but I'm curious as to why you are using dynamic here. Having a List<dynamic> basically means you don't care what the type of the incoming list is. If you really care for the type(seems like you do as you are doing serialization) maybe you shouldn't be using dynamic.

另一方面,我认为您应该重新考虑代码的设计。我没有足够的背景知识,但我很好奇为什么你在这里使用动态。有一个列表 基本上意味着您不关心传入列表的类型。如果您真的喜欢这个类型(看起来就像您在进行序列化一样),那么您可能不应该使用dynamic。

#2


1  

 private void x(List<dynamic> dynamicList)
            {
                Type typeInArgument = dynamicList.GetType();
                Type newGenericType = typeof(List<>).MakeGenericType(typeInArgument);
                IList data = (IList)JsonConvert.DeserializeObject(this._dataSourceValues[i], newGenericType);
            }