.NET Framework Bookmark and Share   
 index > .NET Base Class Library > How to add a property-level attribute dynamically using Reflection?
 

How to add a property-level attribute dynamically using Reflection?

var propertyInfo = typeof(FilterParameter).GetProperty("FilterParameterId");

// how can I do this? propertyInfo.AddAttribute(ReadOnlyAttribute(true))

Many thanks.
  • Edited byDynamic Wednesday, August 05, 2009 10:54 AM
  •  
Dynamic
I think it's impossible to add the attributes dynamically.
You can read this topic.
http://stackoverflow.com/questions/129285/can-attributes-be-added-dynamically-in-c
  • Marked As Answer byDynamic Wednesday, August 05, 2009 3:25 PM
  •  
sUppressor
You can't add attributes. Reflection is read-only on a compiled assembly only.

Why do you want to add the attribute to an existing property? Do you want to compile the assembly again with the new attribute stored?
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
  • Marked As Answer byDynamic Wednesday, August 05, 2009 3:25 PM
  •  
Geert van Horrik
I think it's impossible to add the attributes dynamically.
You can read this topic.
http://stackoverflow.com/questions/129285/can-attributes-be-added-dynamically-in-c
  • Marked As Answer byDynamic Wednesday, August 05, 2009 3:25 PM
  •  
sUppressor
You can't add attributes. Reflection is read-only on a compiled assembly only.

Why do you want to add the attribute to an existing property? Do you want to compile the assembly again with the new attribute stored?
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
  • Marked As Answer byDynamic Wednesday, August 05, 2009 3:25 PM
  •  
Geert van Horrik
This is only relevant to a PropertyGrid. You can customize what it displays with a TypeConverter or a UITypeEditor. Ask questions about this in the Windows Forms Designer forum.

Hans Passant.
nobugz
I am using ASP.NET Dynamic Data and I have to add ReadOnly attribute to one of the fields (UpdatedDate) of my class (FilterParameter).

So I need a solution so that I can add an attribute dynamically or statically on the condition that the original class is not changed (as I am using Entity Framework). I read about MetadataType but still can't make it work.

I can supply code if you want.
  • Edited byDynamic Wednesday, August 05, 2009 12:27 PM
  •  
Dynamic
No idea what "dynamic data" means. You can't add attributes to types that are already compiled. I would recommend you post to a more suitable forum, either at forums.asp.net or at the Entity Framework forum at this site.

Hans Passant.
nobugz
Thanks.

Well, I wanted the answer to my original question about C# which is why I posted it here.
Dynamic
If you are just trying to do that, consider inheriting from PropertyInfo Class and overriding GetAccessors Method.
This would limit reading the object to your custom class but will give you the functionality you need.
Gal Ratner

You can use google to search for other answers

Custom Search

More Threads

• Generics...Sorting a list of objects
• How to limit the textbox input to numerical only?
• Build number is not changing on rebuilds of project
• Convert array<Char>^ to a String^
• Problem accessing the certificate store on W2000 machines
• how to set timeout in VB.NET
• using instruction question
• Trim a Double variable to two decimal spaces?
• Problem using reflection to retrieve values of a sub object
• Which classes call FIPS-certified algorithm code?