- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 C# 反射获取和设置通用对象内通用字段的值。但是我找不到任何方法来获取这些字段的属性值。下面的代码示例:
public class Foo<T>
{
public bool IsUpdated { get; set; }
}
public abstract class ValueObjec<T>
{
public string InnerValue { get; set; }
}
public class ItemList: ValueObject<ItemList>
{
public Foo<string> FirstName;
public Foo<string> LastName;
}
问题在 (*) 行获取“空”项。
itemField.GetType() 总是返回 System.Reflection.RtFieldInfo 类型而不是 Foo 类型。
我已经尝试使用 itemField.FieldType.GetProperty("IsUpdated"),它在返回正确的属性时起作用。但是每当调用 GetValue() 方法时都会抛出错误 “对象与目标类型不匹配。”itemField.FieldType.GetProperty("IsUpdated").GetValue(itemField, null)
如果能得到任何人的帮助,将不胜感激!
var itemList = new ItemList();
foreach (var itemField in itemList.GetType().GetFields())
{
var isUpdated = "false";
var isUpdatedProp = itemField.GetType().GetProperty("IsUpdated"); // (*) return null from here
if (isUpdatedProp != null)
{
isUpdated = isUpdatedProp.GetValue(itemField, null).ToString();
if (isUpdated == "false") isUpdatedProp.SetValue(itemField, "true");
}
}
foreach (var itemField in itemList.GetType().GetFields())
{
var isUpdated = "false";
var isUpdatedProp = itemField.FieldType.GetProperty("IsUpdated");
if (isUpdatedProp != null)
{
isUpdated = isUpdatedProp.GetValue(itemField, null).ToString(); (*) // throw error "Object does not match target type"
if (isUpdated == "false") isUpdatedProp.SetValue(itemField, "true");
}
}
最佳答案
让我们一次打开一个东西:
var isUpdatedProp = itemField.GetType().GetProperty("IsUpdated");
你应该永远需要使用.GetType()
在成员上;你想要.FieldType
或 .PropertyType
(对于属性)。和 nameof
很棒:
var isUpdatedProp = itemField.FieldType.GetProperty(nameof(Foo<string>.IsUpdated));
(string
这是一个假人)
然后:
isUpdated = isUpdatedProp.GetValue(itemField, null).ToString();
这不是 itemField
那就是你的对象——这就是 itemField
的值在那个对象上给你。所以把它传进去;并将结果视为 bool 值,如果可能的话:
var isUpdated = false;
object foo = itemField.GetValue(itemList);
...
isUpdated = (bool)isUpdatedProp.GetValue(foo, null);
最后:
if (isUpdated == "false") isUpdatedProp.SetValue(itemField, "true");
再一次,对象是itemList
,并且该属性不是 string
if (!isUpdated) isUpdatedProp.SetValue(foo, true);
如果你做 Foo<T> : IFoo
会更容易其中 IFoo
是一个非通用接口(interface):
interface IFoo { bool IsUpdated {get; set; } }
然后变成:
var foo = (IFoo)itemField.GetValue(itemList);
if(!foo.IsUpdated) foo.IsUpdated = true;
最后,请注意 FirstName
和 LastName
将是空的如果你没有给他们分配任何东西。
关于c#泛型类无法获取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49278817/
我有一个加载有默认值的元素。后来,我通过 jQuery 的 input.val("different value") 更改了该值。 . 当我 console.log() 元素时,我在 firebug
我们在 DropDownListFor(ASP.NET MVC3 版本)中发现了奇怪的行为。它在下拉列表中选择 ViewBag 属性值而不是 Model 属性值。 模型: public class C
寻找一种方法将描述字段添加到 Magento 中的单个属性值。请注意,我指的是属性值选项,而不是实际的属性本身。 举个例子: 属性=颜色 属性值:红、绿、蓝 我想为 3 种颜色中的每一种添加一个描述字
我知道如果我们知道注释类,我们可以轻松获取特定的注释并访问其属性。例如: field.getAnnotation(Class annotationClass) 它将返回特定注释接口(interface
我正在尝试报告我创建的椭圆形 div 的边框半径值,但我得到了一个未定义的返回值。谁能解释为什么?我是犯了一个简单的错误还是我的代码有问题?谢谢你! CSS3
我有两个表: Bike__________________________ Kiosk 带列: BikeID, Location_________________ KioskID,
我在 Java .properties 文件中有一个值需要以反冲结束。属性值应该是“\\server\folder\”,我这样输入值: name=\\\\server\\folder\\ 结尾的反斜杠
我创建了一个 DeformableShape 对象并通过 for 循环创建它的实例。我正在调用对象的 setPosition 方法并更改其枢轴属性,但所有实例的值都会更新...假设我有对象 A 并且我
是否可以在类名中为 CSS 传递参数?例如: .mrg-t-X { margin-top: Xpx; } Test 在此示例中,X 应为 10。 最佳答案 不,不是。我们最接近的是 attr()
是否可以在类名中为 CSS 传递参数?例如: .mrg-t-X { margin-top: Xpx; } Test 在此示例中,X 应为 10。 最佳答案 不,不是。我们最接近的是 attr()
是否可以在类名中为 CSS 传递参数?例如: .mrg-t-X { margin-top: Xpx; } Test 在此示例中,X 应为 10。 最佳答案 不,不是。我们最接近的是 attr()
我在使用 C# 中的数据注释时遇到了问题。我正在使用自定义必需属性和范围属性,我想将一个对象设置为错误消息。 [MyOwnRequired(ErrorCode=GlobalMessages.Messa
是否可以在类名中为 CSS 传递参数?例如: .mrg-t-X { margin-top: Xpx; } Test 在此示例中,X 应为 10。 最佳答案 不,不是。我们最接近的是 attr()
我知道如果我们知道注解类,我们可以很容易地得到具体的注解并访问它的属性。例如: field.getAnnotation(Class annotationClass) 这将返回特定注解接口(interf
我正在使用 sinon v4.1.2。根据文档( http://sinonjs.org/releases/v4.1.2/sandbox/ ),我应该能够使用以下内容设置属性: sandbox.stub
我想在我的应用程序中将一些 valraibles 的值外部化,它使用 spring 到类似属性文件的东西。 我怎样才能做到这一点? 最佳答案 Spring 提供了一个 BeanFactoryPostP
我有这个界面 public interface IMyInterface { IEnumerable Params { get; } } 在哪里 MyParamInfo 是 public c
我有一个 xml 字符串,其中包含我想要屏蔽的某些值。我还有一个黑名单列表,其中包含我要屏蔽的元素或属性的名称。我如何使用 Linq 执行此操作? var BlackList=new List{"ss
以下是读入XmlDocument的XML文件 我需要的是存储在一些 TextBox 中的 'id' 属性值(“2015”) 这就是 XmlDocument 的加载方式 XmlDocume
IDE 对象检查器通过下拉 ColorBox 显示 TColor 属性,并且可以按图形单元中定义的名称 - clBlack 等选择颜色。问题是图形单元中定义的 clWeb 颜色不存在,并且我定义的任何
我是一名优秀的程序员,十分优秀!