Sha Sea

Hi,
Im using Visual Studio 2005 and .Net 3.0. I wanted to apply Break Points in my Custom Attribute class. I and my falla both used number of sample code but Neither code is not executing Custom class nor break points work.

Here is someone sample code. Im not even able to apply Break point.
Can any Genious knows the answer

using System;

using System.Collections.Generic;

using System.Text;


namespace RegKeyAttributeTestor

{

[AttributeUsage(AttributeTargets.Method|AttributeTargets.Struct,

AllowMultiple=false,Inherited=false)]

public class MyAttribute:Attribute

{

private string regKey="a12nf";

public MyAttribute(string regKey)

{

if(this.regKey==regKey)

{

Console.WriteLine("Permitted to use this App");

}

else

{

Console.WriteLine("Not registered to use this App");

}

}

} //End Attribute class code

class useAttrib

{

[MyAttribute("hello")]

public static string SayHello(string str)

{

return str;

}

static void Main()

{

Console.WriteLine(SayHello("Hello to Sufyan"));

Console.ReadLine();

}

}




Thanks ,
sha


Re: Visual C# General Custom Attribute class

H. Tony

I dont think Attribute is supposed to be used this way.

Try the following code in your Main() and your will see your attribute called.

Code Snippet

useAttrib testClass = new useAttrib();

Type type = testClass.GetType();

foreach (MethodInfo mInfo in type.GetMethods())

{

if (mInfo.Name == "SayHello") Attribute.GetCustomAttributes(mInfo);

}

Above is modified based on MSDN entry.






Re: Visual C# General Custom Attribute class

Sha Sea

Thanks for the answer.

The code which i used is a one way of using custom attribute .

But its creating a problem.






Re: Visual C# General Custom Attribute class

ChunSheng Tang - MSFT

Hi, sha

An instance of the custom attribute is not initated thus the constructor of the customer attribute is not called. If you add the following line to your main method then the constructor of MyAttribute will be executed.

Code Snippet
typeof(useAttrib).GetMethod("SayHello").GetCustomAttributes(typeof(MyAttribute), false);

The custom attribute is intent to associate information to its target element. I don't think it's a proper manner to execute any processing logic in it.

Best Regards

Chunsheng Tang






Re: Visual C# General Custom Attribute class

Sha Sea

Hi Chunsheng,

Thanks for the reply but u r getting the associated information attached with SayHello() method by MyAttribute.

I want to execute the constructor of MyAttribute class by calling the method SayHello().

It should work like that because MyAttribute is associate with SayHello(). Since it is associate , it should execute constructor of MyAttribute().

Best Regards & Thanks,

sha






Re: Visual C# General Custom Attribute class

H. Tony

IMHO, actually even though it's associated, it should NOT execute contractor of MyAttribute() unless it's called since that's how attribute works.




Re: Visual C# General Custom Attribute class

Sha Sea

Thanks tony.

Ok than how can i execute keeping the same scenario Actually i want to act like .Net buil-in attributes.
Do i have to register something See .Net library also doing the same thing. How it can be achieved

Thanks,

sha






Re: Visual C# General Custom Attribute class

OmegaMan

Sha Sea wrote:

Thanks tony.

Ok than how can i execute keeping the same scenario Actually i want to act like .Net buil-in attributes.
Do i have to register something See .Net library also doing the same thing. How it can be achieved

Thanks,

sha



YOu need to use reflection during runtime to get the attributed information from the method. I discuss such scenarios in my blog entitled C# Using Extended Attribute Information on Objects.