views:

41

answers:

1
[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?

A: 

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
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