Can an attribute be used to override a method?

For example, is it possible to mark the Human class with the PetOwner attribute, which overrides the constructor with something that creates the Dog object, and also calls the basic Human constructor?

(And overrides the "Human" GoForWalk () method to enable interaction with your dog?)

I'm new to Reflection and I was curious, so I thought I might ask. Is it related to Reflection.Emit somehow?

I've also looked at this question, but I don't think it is completely related to what I am asking (and I don't understand it either).

Thanks for any help! :)

EDIT: This question is similar to what I'm going to

+3


source to share


1 answer


Yes and no, it depends on different aspects of what you really want to achieve, so a little more information will probably be helpful. Since you are asking about reflection, it gives me the impression that you have existing libraries that you want to extend with your own functionality, but that is not clear from your explanation.

The .NET Framework and C # allow you to use different ways to override code / change behavior, but it all depends on when information to override is available; at design time (-> typical OO construct), after compile time (-> aspect-oriented programming), or at compile time (-> reflection / dynamic functions).



In most cases, reflection is the last resort because it inherently has performance to deal with (unless, of course, in addition to using reflection to read attributes, you also need to write additional code and execute it). That being said, Reflection.Emit is really a way to override available methods (by emitting a method with the same name and signature).

+2


source







All Articles