gpt4 book ai didi

c# - 属性名称并需要它的值

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:54 24 4
gpt4 key购买 nike

我有一个属性的名称,需要在类中找到它的值,获得该值的最快方法是什么?

最佳答案

我假设您在运行时拥有该属性的名称;不是在编码时...

假设您的类名为 TheClass,它有一个名为 TheProperty 的属性:

object GetThePropertyValue(object instance)
{
Type type = instance.GetType();
PropertyInfo propertyInfo = type.GetProperty("TheProperty");
return propertyInfo.GetValue(instance, null);
}

关于c# - 属性名称并需要它的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/798567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com