Jump to content
  • Advertisement
Sign in to follow this  
Geronimo2000

[.net] Inserting Existing Attributes Into MSIL Constructed Properties

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been writing some functionailty which adds fields, properties, and methods to types defined at run-time via the MSIL OpCodes assembly generation. It's all going OK so far except that I can't find a good way to take an existing property's Attribute and translate or copy it to a property I am creating. There are two functions in the PropertyBuilder class which are named SetCustomAttribute, but they don't seem very usable for what I'm trying to do. Am I using the wrong function, or am I using the right function incorrectly? Is there no good way to do this? Much thanks for any help you guys can provide.

Share this post


Link to post
Share on other sites
Advertisement
While I don't have a solution for you, I'm curious as to your functionality. Can you, for instance, add a method into the code at run time, and immediately call it during execution? Does the method then behave as if it was in the full compilation of the executable the whole time?

Matt

Share this post


Link to post
Share on other sites
Absolutely. I'm using the System.Reflection.Emit functions. I'm currently making new types at run-time and calling functions from them. Maybe you can even add fields, properties, and methods to existing types. I'll have to look into that later... I'm sure the gurus in here know way more about this than me. Anyway, the function signature is a little verbose for methods created at run-time. Since the compiler couldn't successfully parse a function call which is yet to be defined, you basically end up having to call a static function, Invoke(), on the new Type variable,and pass in your object reference, and string which matches the function name, and then your parameters in an object array.

And actually, as a matter of fact, I found the correct set of functions which allow me to basically copy over attributes to the properties I'm creating at run-time. My new problem is the .NET PropertyGrid.

It will not recognize my attributes such as CategoryAttribute or DescriptionAttribute included with the properties. I need these attributes to be read so the grid will look nice. I can successfully retrieve the attributes from the properties in my object instances, so I know they are there. The PropertyGrid uses attributes I have hardcoded (at compile-time) for other (regular) class instances just fine,and when examining the PropertyInfos for the one that works, and the one that doesn't they appear to be identical! This makes little sense to me... Anyone else have this problem, or have any advice. Thanks in advance if you can help at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!