Can we dynamically edit a method attribute using Reflection in C#?

A

Abhi

I am using Reflection to invoke methods dynamically. I have got a special
requirement where I need to pass a value to method by setting the custom
method attribute. As I cannot change the signature of method to pass a new
parameter, I am setting the custom attribute of a given method and then
accessing the attribute from method. Since attribute value is decided at
runtime I want to change the attribute of particular method at runtime.
Following is what I am trying to do:
1.) Load the assembly.
2.) Dynamically set value of custom attribute of a particular method.
3.) Recompile the assembly so that when the particular method is invoked it
takes the updated attribute value.
4.) Invoke the method

I am interested to know how to do Step 2 & 3

I saw lots of example where we can generate the code, create and execute it
dynamically but not a example where we can modify the existing code and then
invoke the method using reflection. Please let me whether what I am trying to
do is possible or not using .Net3.0 or later.
Thanks,
Abhi
 
M

Marc Gravell

That sounds a very odd thing to do... can you add some context (what
you want to achieve)? there may be better ways of doing this.

No that you can't unload an assembly from an AppDomain; once you have
loaded it once, you are stuck with it - you can't really re-compile it
in-place. Even if you could, you would also have issues if that
assembly was signed.

As for .NET 3; note that 3.0 and 3.5 /mainly/ just add some bits -
there are no fundamental changes (except the thread-pool limits ;-p) -
so the same would apply to 2.0 and upwards (all current CLR 2.x
variants).

In particular, depending on what you are doing, the component-model
(and runtime [rather than compile-time] version of reflection) may
offer some options for updating attributes, but there are probably
easier ways of doing it...

So: what are you trying to pass in the attribute? And what is
consuming it? Is it your custom code, or library code (such as Linq-to-
Sql attributes).

Marc
 
A

Abhi

I need to maintain the state of member variable between 2 method calls
through reflection. for example i have following class in assembly whose
method I am invoking it dynamically using reflection

Class A
{
int lang;

public void SetLang(int x)
{
lang = x;
}

public bool VerifyLang()
{
if (lang == 1033)
return true;

return false;
}
}

I need to call VerifyLang() method through reflection but I am not able to
use SetLang(int x) method to set the expected value of "lang" as the state
will not preserved between 2 method calls through reflection in a same way we
do it through object. Can I set the member variable like in this case "lang"
dynamically by creating the object of Type "Class A" and then invoking the
method VerifyLang() using the same object used earlier to set the member
variable so that state is maintained between multiple invoke methods through
reflection.

Please let me know if my requirement is still not clear.
 
M

Marc Gravell

as the state will not preserved between 2 method calls through reflection in
a same way we do it through object.
You've lost me... the state will behave identically regardless of
whether you use reflection or compiled invoke; all you need to do is
make sure you target the same instance [the first arg to Invoke(...)]
- exactly the same as with compiled code.

Do you have any example code of what isn't currently working? It is
probably a simple fix.

Marc
 
A

Abhi

I think you have answered my question. I can maintain the state of member
variable by using the same object between different method invoke call. The
problem was I treating the Reflection CreateInstance(..) object in different
way then normal object and that’s why I looking into weird ways to pass the
value between 2 different methods. Thanks for the help…..

Marc Gravell said:
as the state will not preserved between 2 method calls through reflection in
a same way we do it through object.
You've lost me... the state will behave identically regardless of
whether you use reflection or compiled invoke; all you need to do is
make sure you target the same instance [the first arg to Invoke(...)]
- exactly the same as with compiled code.

Do you have any example code of what isn't currently working? It is
probably a simple fix.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top