I have created 3 attributes ClassAttribute, PropAttribute and InstanceAttribute.
My code should do those things check if object has custom attribute typeof(ClassAttribute) then if it is so find all PropAttributes and get their values and check whether contains InstanceAttribute.
Code is simple but I'm not able to do check against InstanceAttribute. My code is below.
// Class Main
var class_att = (ClassAttribute)Attribute.GetCustomAttribute(a_object.GetType(), typeof(ClassAttribute));
if(class_att != null){
var output = PropAttribute.GetOutput(a_object);
}
// End of class Main
//Class PropAttribute
public void GetOutput(input_object){
var props = input_object.GetType().GetMembers()
.Where(prop => IsDefined(prop, typeof(PropAttribute))).ToList();
object obj = null;
dynamic dynObj = null;
InstanceAttribute attritube = null;
foreach(var p in props){
//Lets say that I will use only Property type
obj = ((PropertyInfo)comp).GetValue(input_object);
dynObj = Convert.ChangeType(obj, obj.GetType()); //Here is the object for which I was looking for
attritube = GetCustomAttribute(dynObj, typeof(InstanceAttribute)); //**This check fails (check error below)**
}
}
// End of class PropAttribute
ERROR: RuntimeBinderException: The best overloaded method match for 'System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type)' has some invalid arguments (wrapper dynamic-method)
Please can you help me?
Aucun commentaire:
Enregistrer un commentaire