2
[CustomAttribute]
public bool IsGreen()
{
   return true;
}

How could one write the above using a DynamicMethod in c#?

UPDATE; per casperOne you cannot do this with a custom attribute.

But what about a non-custom attribute such as:

[Conditional("DEBUG")]
public bool IsGreen()
{
   return true;
}

Note: I created a new post, because my last one missed the point which is: What im driving at is...how do i dynamically create a method that contains an attribute?

Also, i asked about using DynamicMethod, is there a better way?

Hawken
  • 2,059
  • 19
  • 34
schmoopy
  • 6,419
  • 11
  • 54
  • 89

1 Answers1

0

You cannot. From the note in the remarks section for the documentation for the IsDefined method on the DynamicMethod class:

Custom attributes are not currently supported on dynamic methods.

If you want to create dynamic methods then you will have to create an assembly/module/type/method dynamically and then attach the attributes to that.

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • I appreciate your answer, im trying to wrap my head around what can/cannot be done and how. What about a method with a non-custom attribute? Im updating my post above. – schmoopy Aug 09 '10 at 15:52