Click here to Skip to main content
15,607,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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."
Updated 12-Jul-13 21:52pm
Mahesh Bailwal 13-Jul-13 3:42am    
Debug and find at which line you are getting this error and then update your question with more information?
hadisalehy 13-Jul-13 3:49am    
In this line:
object defaultObject = defaultProperty.GetValue(assembly, null);
Error Message is: "Object does not match target type."
Mahesh Bailwal 13-Jul-13 4:16am    
what are you trying to do?
hadisalehy 13-Jul-13 5:14am    
I want to get a dll file and take properties , read property value
Mahesh Bailwal 13-Jul-13 5:40am    
Are you trying to read custom attributes or normal property?

1 solution

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)[^]
Share this answer
hadisalehy 14-Jul-13 0:43am    
Mahesh Bailwal Thanks for your answer
But this episode works well
You can try any kind of attributes.
var propList = form.GetProperties().Where(p => p.GetCustomAttributes(typeof(customAttribute), true).Length != 0).ToList();

I found this error reason.
GetValue method is to use the create instance, in this way we are unable to create instance for getvalue.
Is there a way to get the value of the property?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900