472,374 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

Attribute that is called by compiler during compilation?

I suspect it's unlikely, but you'll never know until you ask, but are there any attributes that would allow us to contribute to
the compiled output?

e.g.

// this class exists in a separate, compiled assembly for the compiler to load during compilation
class MyMethodCompiler : Attribute
{
// This is called during compilation
public override void GenerateMethod(MethodBuilder method)
{
// modify the IL that was generated from the compiler
}
}
// some class in a user assembly
class MyClass
{

[MyMethodCompiler]
public void SomeMethod(...)
{
// User code in method
...
...
...
}
}
May 4 '06 #1
5 2122
Alas no. This is not the goal of an attribute.

Code cannot be run at compile time because, well, the code hasn't been built
yet...

Attributes provide metadata which may be used at runtime but is only
available through reflection on a finished assembly or an instance of a
class. The Attribute based classes can have functional methods but are
usually "instantiated" by the process of reflection and an attribute cannot
alter the form of the code with which it is associated.

An attribute that has construction parameters will be created, instantiated,
when the TypeDescriptor.GetAttributes method is called. An attribute with no
constructor parameters will be instantiated at the moment that the
reflection process discovers an attribute of that type in the metadata. If
the attribute is way down on the list, the construction will take place
after the previous attributes have been created.

Once an attribute is instantiated by the process of reflection you can then
call its methods.

Find a little C# example after my signature...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace StrangeAttribute

{

/// <summary>

/// Summary description for Form1.

/// </summary>

[

Pork(),

Pork("......Plonk")

]

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(96, 128);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "Reflect";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

System.Diagnostics.Trace.WriteLine("Reflecting");

foreach(Attribute a in TypeDescriptor.GetAttributes(this,true))

{

System.Diagnostics.Trace.WriteLine("Got attribute: "+a.ToString());

if(a.GetType() == typeof(PorkAttribute))

{

((PorkAttribute)a).foo();

}

}

}

}

[

AttributeUsage(AttributeTargets.All, AllowMultiple=true)

]

public class PorkAttribute : Attribute

{

public PorkAttribute()

{

this.Arrgghh("!!");

}

public PorkAttribute(string stuff)

{

MessageBox.Show("This time it worked"+stuff);

}

void Arrgghh(string stuff)

{

MessageBox.Show("Attribute Constructed!!");

}

public void foo()

{

MessageBox.Show("Foo!!");

}

}

}

"Stuart Carnie" <st***********@nospam.nospam> wrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
I suspect it's unlikely, but you'll never know until you ask, but are there
any attributes that would allow us to contribute to the compiled output?

e.g.

// this class exists in a separate, compiled assembly for the compiler to
load during compilation
class MyMethodCompiler : Attribute
{
// This is called during compilation
public override void GenerateMethod(MethodBuilder method)
{
// modify the IL that was generated from the compiler
}
}
// some class in a user assembly
class MyClass
{

[MyMethodCompiler]
public void SomeMethod(...)
{
// User code in method
...
...
...
}
}

May 4 '06 #2
What about the ConditionalAttribute - this does indeed affect compilation and resulting output.

Try the following code and use Lutz's Reflector - you will see CSC.exe was indeed influenced by the [Conditional] attribute.
There is no call to test.MethodTwo() in the resulting IL.

A condition would be the attribute would be compiled in a separate assembly, if it is going to be executed by the compiler, but
given my example below, certainly possible.

************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyTest test = new MyTest();

test.MethodOne();
test.MethodTwo();
}

}

class MyTest
{
public void MethodOne()
{
Console.WriteLine("MethodOne");
}

[Conditional("NOCOMPILE")]
public void MethodTwo()
{
Console.WriteLine("MethodTwo");
}
}
}

************************************
Bob Powell [MVP] wrote:
Alas no. This is not the goal of an attribute.

Code cannot be run at compile time because, well, the code hasn't been built
yet...

Attributes provide metadata which may be used at runtime but is only
available through reflection on a finished assembly or an instance of a
class. The Attribute based classes can have functional methods but are
usually "instantiated" by the process of reflection and an attribute cannot
alter the form of the code with which it is associated.

An attribute that has construction parameters will be created, instantiated,
when the TypeDescriptor.GetAttributes method is called. An attribute with no
constructor parameters will be instantiated at the moment that the
reflection process discovers an attribute of that type in the metadata. If
the attribute is way down on the list, the construction will take place
after the previous attributes have been created.

Once an attribute is instantiated by the process of reflection you can then
call its methods.

Find a little C# example after my signature...

May 4 '06 #3
"certainly possible" suggests in a future version :) I it doesn't appear to be available today, but you never know what the
future holds.

Cheers,

Stu

Stuart Carnie wrote:
What about the ConditionalAttribute - this does indeed affect
compilation and resulting output.

Try the following code and use Lutz's Reflector - you will see CSC.exe
was indeed influenced by the [Conditional] attribute. There is no call
to test.MethodTwo() in the resulting IL.

A condition would be the attribute would be compiled in a separate
assembly, if it is going to be executed by the compiler, but given my
example below, certainly possible.

************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyTest test = new MyTest();

test.MethodOne();
test.MethodTwo();
}

}

class MyTest
{
public void MethodOne()
{
Console.WriteLine("MethodOne");
}

[Conditional("NOCOMPILE")]
public void MethodTwo()
{
Console.WriteLine("MethodTwo");
}
}
}

************************************
Bob Powell [MVP] wrote:
Alas no. This is not the goal of an attribute.

Code cannot be run at compile time because, well, the code hasn't been
built yet...

Attributes provide metadata which may be used at runtime but is only
available through reflection on a finished assembly or an instance of
a class. The Attribute based classes can have functional methods but
are usually "instantiated" by the process of reflection and an
attribute cannot alter the form of the code with which it is associated.

An attribute that has construction parameters will be created,
instantiated, when the TypeDescriptor.GetAttributes method is called.
An attribute with no constructor parameters will be instantiated at
the moment that the reflection process discovers an attribute of that
type in the metadata. If the attribute is way down on the list, the
construction will take place after the previous attributes have been
created.

Once an attribute is instantiated by the process of reflection you can
then call its methods.

Find a little C# example after my signature...

May 4 '06 #4
Stuart,
A condition would be the attribute would be compiled in a separate assembly, if it is going to be executed by the compiler, but
given my example below, certainly possible.


But the ConditionalAttribute itself doesn't do anything here. It's
simply a hint to the compiler to possibly exclude calls to the method.
All the logic is in the compiler, the attribute is just a dumb class.

You could do the same today for your own attribute by extending one of
the open source C# compilers.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 4 '06 #5
Indeed - I'm suggesting a 'future' feature. An extensible compiler. How useful would that be!

Mattias Sjögren wrote:
Stuart,
A condition would be the attribute would be compiled in a separate assembly, if it is going to be executed by the compiler, but
given my example below, certainly possible.


But the ConditionalAttribute itself doesn't do anything here. It's
simply a hint to the compiler to possibly exclude calls to the method.
All the logic is in the compiler, the attribute is just a dumb class.

You could do the same today for your own attribute by extending one of
the open source C# compilers.
Mattias

May 5 '06 #6

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

Similar topics

5
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
2
by: Mike Fisher | last post by:
I'm seeing an error when I try to run/debug a web service. Although it doesn't happen every time, it does occur more than half of the times I hit F5. It appears to be returned by the the JIT...
4
by: Julia | last post by:
Hi, Is there any why I could use attribute or something else to instruct the compiler to inject a specific code for a property? for example i have the following property and i would like that...
1
by: Skip | last post by:
Hi, I created a COM object in C# and everything works fine on my machine. Now, the compiler gives me the following messages when compiling my code: ------ Rebuild All started: Project: SI,...
1
by: Invalidlastname | last post by:
Hi, Our developer team recently started getting the compilation error, see below, once a while running the asp.net web application from Visual Studio 2003 (in debug mode), and we have to rebuild the...
9
by: JTrigger | last post by:
When I compile my project using the IDE on a development machine it works just fine. When I compile it on the server using csc.exe, I get the following error when I try to bring it up in the web...
1
by: christian.Blackburn | last post by:
Hi Gang, On my development system I am not getting this error, but it does happen when I upload it to my ISP. Can someone tell me why this is happening? I don't have any user-defined controls...
1
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
3
by: dancer | last post by:
I am using Framework 1.1.4322. Who can tell me why I'm getting this error? My code follows Compilation Error Description: An error occurred during the compilation of a resource required to...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.