.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Field Attribute & Code Execution
 

Field Attribute & Code Execution

Hi, I'm fairly new to attributes programming and don't exactly know its limitations.

What I'm hoping to acheive isto have a certain piece of code executed when a field is being assigned toby simply decorating it. Now I understand I can do this by simply usingthe setter on a property but that's not what I want. Imagine a struct or class where I could define an OnChange and have it triggerred onlycertain valuesare changed.

I'm not sure if this is possible, my understanding is that attributes are only meant to be read through reflection and then acted upon, is this true?

I know other attributes run code as you call them. For example decorating a method signature with DllImportAttribute will actually invoke code to load and execute the unmanagedapi when you make a call to the decorated method.

Any ideas?

Thanks in advance
Nic

Nicolas L
That's not how attributes work. Invoking the attribute constructor requires explicit code, it doesn't happen automatically. You have to call Type.GetCustomAttributes(). While it may look like the [DllImport] attribute somehow gets automatically invoked, it is actually the compiler that uses it. It generates the proper IL to make the P/Invoke call, guided by the [DllImport] attributes you supplied. The attributes play no role at runtime. That would be very slow, System.Reflection is expensive.

Hans Passant.
  • Proposed As Answer byStephen Cleary Wednesday, June 03, 2009 1:04 PM
  • Marked As Answer byNicolas L Wednesday, June 03, 2009 5:48 PM
  •  
nobugz
That's not how attributes work. Invoking the attribute constructor requires explicit code, it doesn't happen automatically. You have to call Type.GetCustomAttributes(). While it may look like the [DllImport] attribute somehow gets automatically invoked, it is actually the compiler that uses it. It generates the proper IL to make the P/Invoke call, guided by the [DllImport] attributes you supplied. The attributes play no role at runtime. That would be very slow, System.Reflection is expensive.

Hans Passant.
  • Proposed As Answer byStephen Cleary Wednesday, June 03, 2009 1:04 PM
  • Marked As Answer byNicolas L Wednesday, June 03, 2009 5:48 PM
  •  
nobugz
Ok, that makes sense, and it goes along the lines of what I was thinking. Then is there a way to have the IL compile custom code for me when a field is decorated with a certain attribute? The same way it does for the DLLImport?

Thanks!

Nic
Nicolas L
IL doesn't compile custom code, compilers do. Yes, you can write a compiler that interprets attributes and have it generate custom IL. It is a fairly common technique, tools like Spring.NET work this way.

Hans Passant.
nobugz
Sorry, rereading my question... it was awfully written.

I guess I'm still trying to reach my goal through having the compiler compile custom code which would do the OnChange for me (I would have to write this).I'm using Visual Studio 2005. Does its compiler support this, or does the language allow a user to have specific code be compiled based on which attribute is used?

Hope this makes more sense

Thanks

Nic
Nicolas L
Let's assume you've written a tool that lets you modify the IL generated by the compiler. Then you could write code like this:

public event EventHandler Change;
protected void OnChange() {
if (Change != null) Change(this, new EventArgs());
}

[PleaseInjectChangeEvent("mBackingField")]
public int SomeProperty {
get { return mBackingField; }
set { mBackingField = value; }
}

If you don't have this tool, you would write this:

public int SomeProperty {
get { return mBackingField; }
set {
if (mBackingField != value) {
OnChange();
mBackingField = value;
}
}
}

One line of code extra.


Hans Passant.
nobugz

This is a very good lead. Thank you very much.

Nic

Nicolas L

You can use google to search for other answers

Custom Search

More Threads

• Whay do i get only the last pattern written on the file?
• SoundPlayer class(es) - -Pause and extra functions
• area covered by an colors
• How to get Locked file or File in use By the Windows Service Process
• Extend Double type
• MethodBase.GetCurrentMethod().DeclaringType throwing an exception (.NET 3.5, VS 2008) "Cannot evaluate security function"
• Can you convert a string to a type?
• Timer in C#
• Question about Smart Client Software Factory
• How to store and clone PrinterSettings?