当我有类但子类名称为字符串时,如何从子类获取属性 [英] How do I get the properties from a subclass when I have the class but subclass name as a string
问题描述
我有一个方法可以从错误消息中获取子类的名称,如下所示
I have a method which gets the name of a sub class from an error message, as below
string jobDesc = getDtlName(serviceResponse.ErrorMessages[0].Error);
我有Class,并且想访问子类的属性,但是当我仅将子类的名称作为字符串时,该怎么办?
I have the Class and want to access the sub class properties but how can I do that when I only have the sub class name as a string.
Java有一个像这样的方法
Java have a method which works like this
Class myClass = Class.forName(jobDesc);
如何在C#中做同样的事情?
How can I do the same in C#?
推荐答案
我设法通过Properties和下面的代码获得了可行的解决方案
I managed to get a working solution using Properties with the code below
PropertyInfo[] properties = typeof(UIStaticFieldErrors).GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.ToString() == "Boolean " + jobDesc)
{
property.SetValue(errorSF, true);
}
}
我在属性中获取UIStaticFieldErrors的属性,然后进行迭代,直到获得jobDesc的匹配项,然后将该字段更新为true.变量errorSF是UIStaticFieldErrors的实例.
I get the properties of UIStaticFieldErrors in properties and then iterate through until I get a match for jobDesc, then I update that field to true. The variable errorSF is an instance of UIStaticFieldErrors.
这篇关于当我有类但子类名称为字符串时,如何从子类获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!