使用C#中的反射从程序集中获取属性值 [英] Get property value from assembly using reflection in C#

查看:50
本文介绍了使用C#中的反射从程序集中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了这段代码,但是它给出了错误信息!



I checked this code, but it gives an error message!

// load assembly
          System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(openFileDialog1.FileName);
          var settingsType = assembly.GetTypes();
          foreach (var form in settingsType)
          {
              //var propList = form.GetProperties().Where(p => p.GetCustomAttributes(typeof(Aviation.Library.CS.Libs.TransactionAttribute), true).Length != 0).ToList();
              var propList = form.GetProperties().ToList();

              foreach (PropertyInfo btn in propList)
              {

                  System.Reflection.PropertyInfo defaultProperty = form.GetProperty(btn.Name);
                  object defaultObject = defaultProperty.GetValue(assembly, null);
              }
          }





错误信息是:

对象与目标类型不匹配。



The error message is:
"Object does not match target type."

推荐答案

我相信您用于读取自定义属性的代码不正确。请参阅以下链接阅读自定义属性



使用反射访问属性(C#编程指南) [ ^ ]
I believe code you are using for reading custom attributes is not correct. Pleas refer below link to reading custom attributes

Accessing Attributes With Reflection (C# Programming Guide)[^]


这篇关于使用C#中的反射从程序集中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆