简体   繁体   English

我需要获取特定对象属性的值,但不知道 object 的类型

[英]I need to get a value of specific object's property, but don't know the type of the object

I have got an c# object and I don't know the type of this object.我有一个 c# object 我不知道这个 object 的类型。 (ie object o) What I know is that this object has a member called 'ID' of type int. (即 object o)我所知道的是,这个 object 有一个名为“ID”的 int 类型的成员。

I wanted to get the value of this property but I am not good enough with reflection...我想获得这个属性的价值,但我的反射还不够好......

I can get a type and members of this object:我可以得到这个 object 的类型和成员:

Type type = obj.GetType();
System.Reflection.MemberInfo[] member = type.GetMember("ID");

...but don't know what to do next:-) ...但不知道下一步该怎么做:-)

Thanks for help in advance Mariusz提前感谢马里乌斯的帮助

You can use:您可以使用:

Type type = obj.GetType();
PropertyInfo property = type.GetProperty("ID");
int id = (int) property.GetValue(obj, null);
  • Use PropertyInfo because you know it's a property, which makes things easier使用PropertyInfo因为您知道它是一个属性,这使事情变得更容易
  • Call GetValue to get the value, passing in obj as the target of the property and null for indexer arguments (as it's a property, not an index)调用GetValue获取值,传入obj作为属性的目标, null用于索引器 arguments(因为它是属性,而不是索引)
  • Cast the result to int as you already know it's going to be an int将结果转换为int ,因为您已经知道它将是一个int

Jared's suggestion of using dynamic is good too, if you're using C# 4 and .NET 4, although to avoid all the brackets I'd probably write it as: Jared 关于使用dynamic的建议也很好,如果您使用的是 C# 4 和 .NET 4,尽管为了避免使用所有括号,我可能会将其写为:

dynamic d = obj;
int id = d.ID;

(unless you needed it in a single expression for some reason). (除非您出于某种原因需要在单个表达式中使用它)。

Is this a public property?这是公共财产吗? Is so then the easiest route is to use dynamic是这样的话,最简单的路线是使用dynamic

int value = ((dynamic)obj).ID;

Can you use C# 4?你能用 C# 4 吗? In that case, you can use dynamic :在这种情况下,您可以使用dynamic

dynamic dyn = obj;
int id = dyn.ID;
public class TestClass
{
    public TestClass()
    {
        // defaults
        this.IdField = 1;
        this.IdProperty = 2;
    }

    public int IdField;
    public int IdProperty { get; set; }
}

// here is an object obj and you don't know which its underlying type
object obj = new TestClass();
var idProperty = obj.GetType().GetProperty("IdProperty");
if (idProperty != null)
{
    // retrieve it and then parse to int using int.TryParse()
    var intValue = idProperty.GetValue(obj, null);
}

var idField = obj.GetType().GetField("IdField");
if (idField != null)
{
    // retrieve it and then parse to int using int.TryParse()
    var intValue = idField.GetValue(obj);
}

暂无
暂无

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

相关问题 当我不知道类型时,可以获取属性的DataAnnotation显示名称吗? - Can I get the DataAnnotation Display Name of a property when I don't know the type? 如何比较两个 IEnumerable<T> 在 C# 中,如果我不知道实际的对象类型? - How to compare two IEnumerable<T> in C# if I don't know the actual object type? 如何将对象转换为C ++ / CLI中不知道其类型参数的通用列表? - How can I cast an Object to a generic List I don't know the type parameter of in C++/CLI? 如果我不知道正确的类型,如何反序列化对象? - How can I deserialize an object if I don't know the right type? 需要在值类型对象中使用随机数生成器,但不希望其依赖性负担 - Need to use random number generator in a value type kind of object but don't want the burden of its dependency 如何投放清单 <Object> 列出 <Foo> 当foo是类型变量并且我不知道类型时 - How can I cast List<Object> to List<Foo> when foo is a type variable and I don't know the type 从List获取特定类型的对象<T> - Get specific type of object from List<T> 自动映射 - 当源对象具有值的属性时,不映射 - Automapper - Don't map when a source object has a property with a value 当 c# 的值为 null 时,c# 可以知道 object 的类型吗? - Can c# know the type of an object when it's value is null? 对于任何值类型T,如何确定对象的类型是否为IEnumerable <T>的子类? - How do I find out whether an object's type is a subclass of IEnumerable<T> for any value type T?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM