Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DTO反射_C#_Reflection_Dto - Fatal编程技术网

C# DTO反射

C# DTO反射,c#,reflection,dto,C#,Reflection,Dto,我有一个dto,statEMailDTO,它有一个字段,保存我要查找的内容的字段名(它们以逗号分隔) var emailParams = statEmailDTO.EmailParam.ToString().Split(','); for (int i = 0; i < emailParams.Length; i++) { var fieldName = emailParams[i].ToString(); var-emailParams=statEmailDTO.EmailPar

我有一个dto,
statEMailDTO
,它有一个字段,保存我要查找的内容的字段名(它们以逗号分隔)

var emailParams = statEmailDTO.EmailParam.ToString().Split(',');

for (int i = 0; i < emailParams.Length; i++) {
  var fieldName = emailParams[i].ToString();
var-emailParams=statEmailDTO.EmailParam.ToString().Split(',');
对于(int i=0;i
等等

但是,那么我如何使用反射来获取``fieldName的实际值,该值可以在不同的DTO中找到,siDTO

假设
fieldName=“SuggestionItemID”
,那么我需要获取
siDTO.SuggestionItemID
的值

我过去没有做过很多反思。当然,我读过PropertyInfo,但它只是没有点击

想法?

像这样:

PropertyInfo property = typeof(SomeType).GetProperty(fieldName);
object value = property.GetValue(instance, null);

但是,什么是SomeType?什么是instance?@Patrick:
SomeType
是拥有属性的类型,
instance
是要获取其值的实例。如果我执行以下操作…var fieldName=emailParams[I].ToString();System.Reflection.PropertyInfo property=typeof(SuggestionItemDTO).GetProperty(fieldName);object fieldValue=property.GetValue(fieldName,null);然后我得到一个“object与目标类型不匹配”错误。想法?我的意思是我真的想要siDTO的值。“fieldName”我得到了!Holly crap:System.Reflection.PropertyInfo property=typeof(SuggestionItemDTO)。GetProperty(fieldName);object fieldValue=property.GetValue(siDTO,空);