[.net] Inserting Existing Attributes Into MSIL Constructed Properties

Started by
1 comment, last by Geronimo2000 19 years, 6 months ago
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.
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
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
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.

This topic is closed to new replies.

Advertisement