c# - Bizarre behavior with custom attributes and GetCustomAttributes -
c# - Bizarre behavior with custom attributes and GetCustomAttributes -
i've been fighting problem hours , not find related on (or google matter).
here's problem: have custom attribute contains object array property.
[system.attributeusage(system.attributetargets.property, allowmultiple = false)] public class property : system.attribute { public object[] parameters { get; set; } public jsonproperty(object[] prms = null) { parameters = prms; } }
and utilize next code read properties:
var customproperties = (property[])currentproperty.getcustomattributes(typeof(property), false);
this works fine following:
[property(parameters = new object[]{}] <...property...>
however, if set null ([property(parameters = null])
, error:
system.reflection.customattributeformatexception: 'parameters' property specified not found.
which absurd, because property defined within custom attribute. don't it.
so question is: going on?
--edit
if alter type of property object[] object, assigning null works fine.
--edit add together code
attribute:
[system.attributeusage(system.attributetargets.property, allowmultiple = false)] public class jsonproperty : system.attribute { public object[] parameters { get; set; } public jsonproperty(object[] prms = null) { parameters = prms; } }
class:
public class myclass { [jsonproperty(parameters = null)] public datetime start { get; set; } }
method:
public string getattributes() { type t = myclass.gettype(); // public properties , have been set. var properties = t.getproperties(bindingflags.public | bindingflags.instance) .where(prop => prop.getvalue(this, null) != null); foreach (var prop in properties) { //the error occur on next line. var jsonproperties = (jsonproperty[])prop.getcustomattributes(typeof(jsonproperty), false);
--if didn't understand, seek reading this:
https://social.msdn.microsoft.com/forums/vstudio/en-us/ddebbec6-1653-4502-9802-0b421efec60d/an-unexplicable-customattributeformatexception-from-getcustomattributes?forum=csharpgeneral
i asked question there too.
c# custom-attributes getcustomattributes
Comments
Post a Comment