473,549 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Dec 10 '07 #1
4 6804
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
Dec 10 '07 #2
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.
"Marc Gravell" wrote:
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
Dec 10 '07 #3
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
Dec 10 '07 #4
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" wrote:
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
Dec 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3299
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It is possible to Add and Delete rows - Some cells have special attributes (readonly and events) Here's a snippet of the code:
1
2369
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right direction. First my question: How am I supposed to make instances of delegates when I have the method (that I want to make the delegate of) retrieved...
6
1910
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create a new thread to display a customer on the screen for example. However, I have a problem with circular references in my objects which means that I...
2
1745
by: Bilz | last post by:
Ok, lets say I have a generic method: public T Testing<T>() : where T:class { T myT = getStuff() as T; // Do something with myT return myT; } How do I call it dynamically? The following code does not work, but is
6
11060
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of...
5
8566
by: marfi95 | last post by:
I have a form that has a left and right panel. In the left panel is a treeview. The right panel I want to change dynamically based on the type of node selected. What I'm doing is loading the treeview nodes through an XML file. As part of each node in the XML, I'm using an attribute that indicates the name of a sub form to load. As part...
2
5095
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
15
8177
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a Delegate. My testing showed that actually it was six times faster: 0.5 seconds for 100,000 iterations versus 3.1 seconds. Can anyone explain why? ...
7
10045
by: chage | last post by:
Hi, I have been searching around to try adding reference assembly to another assembly during runtime, programatically. Is this possible in .Net? The reason for this is because i am having trouble using a library that creates an instance of a Type that i specified, and it failed the locate the Type during runtime, if i do not reference it...
0
7472
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7743
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3518
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.