about attribute

T

Tony Johansson

Hello!

I have one simple question about attribute.
According to the documentation will GetCustomAttributes return all the
custom attributes for this assembly
public virtual Object[] GetCustomAttributes (bool inherit)

I just wonder if I have attribute on classes or on method will these be
included and returned when using
this GetCustomAttributes

//Tony
 
N

Nicholas Paldino [.NET/C# MVP]

Tony,

Yes, they will, of course, what is returned depends on what you pass for
the inherit parameter.
 
T

Tony Johansson

Hello!

Here I have a simple example when I use this GetCustomAttributes but
in this example GetCustomAttributes didn't returned the ObsoleteAttribute
that is used on the method
Foo below.

What is the reason that Obsolete was not returned ?


class Program
{
static void Main(string[] args)
{
new Program().Foo();
Assembly a =
Assembly.LoadFrom(@"F:\C#\ConsoleApplication15\ConsoleApplication15\bin\Debug\ConsoleApplication15.exe");
object[] attributes = a.GetCustomAttributes(true);
}

[Obsolete("Just testing")]
public void Foo()
{}
}


//Tony


Nicholas Paldino said:
Tony,

Yes, they will, of course, what is returned depends on what you pass
for the inherit parameter.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tony Johansson said:
Hello!

I have one simple question about attribute.
According to the documentation will GetCustomAttributes return all the
custom attributes for this assembly
public virtual Object[] GetCustomAttributes (bool inherit)

I just wonder if I have attribute on classes or on method will these be
included and returned when using
this GetCustomAttributes

//Tony
 
N

Nicholas Paldino [.NET/C# MVP]

You aren't seeing it because you are getting the custom attributes on
the assembly, not on the method. If you want to get the attribute that is
applied to the particular method, you have to get the MethodInfo instance
and call GetCustomAttributes on that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tony Johansson said:
Hello!

Here I have a simple example when I use this GetCustomAttributes but
in this example GetCustomAttributes didn't returned the ObsoleteAttribute
that is used on the method
Foo below.

What is the reason that Obsolete was not returned ?


class Program
{
static void Main(string[] args)
{
new Program().Foo();
Assembly a =
Assembly.LoadFrom(@"F:\C#\ConsoleApplication15\ConsoleApplication15\bin\Debug\ConsoleApplication15.exe");
object[] attributes = a.GetCustomAttributes(true);
}

[Obsolete("Just testing")]
public void Foo()
{}
}


//Tony


Nicholas Paldino said:
Tony,

Yes, they will, of course, what is returned depends on what you pass
for the inherit parameter.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tony Johansson said:
Hello!

I have one simple question about attribute.
According to the documentation will GetCustomAttributes return all the
custom attributes for this assembly
public virtual Object[] GetCustomAttributes (bool inherit)

I just wonder if I have attribute on classes or on method will these be
included and returned when using
this GetCustomAttributes

//Tony
 
T

Tony Johansson

Hello!

So what you are actually saying is that this use of GetCustomAttributes will
only
return the attributes for the assembly and not attribut for class and method
and so on.

static void Main(string[] args)
{
new Program().Foo();
Assembly a =
Assembly.LoadFrom(@"F:\C#\ConsoleApplication15\ConsoleApplication15\bin\Debug\ConsoleApplication15.exe");
object[] attributes = a.GetCustomAttributes(true);
}



//Tony

Nicholas Paldino said:
You aren't seeing it because you are getting the custom attributes on
the assembly, not on the method. If you want to get the attribute that is
applied to the particular method, you have to get the MethodInfo instance
and call GetCustomAttributes on that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tony Johansson said:
Hello!

Here I have a simple example when I use this GetCustomAttributes but
in this example GetCustomAttributes didn't returned the ObsoleteAttribute
that is used on the method
Foo below.

What is the reason that Obsolete was not returned ?


class Program
{
static void Main(string[] args)
{
new Program().Foo();
Assembly a =
Assembly.LoadFrom(@"F:\C#\ConsoleApplication15\ConsoleApplication15\bin\Debug\ConsoleApplication15.exe");
object[] attributes = a.GetCustomAttributes(true);
}

[Obsolete("Just testing")]
public void Foo()
{}
}


//Tony


Nicholas Paldino said:
Tony,

Yes, they will, of course, what is returned depends on what you pass
for the inherit parameter.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello!

I have one simple question about attribute.
According to the documentation will GetCustomAttributes return all the
custom attributes for this assembly
public virtual Object[] GetCustomAttributes (bool inherit)

I just wonder if I have attribute on classes or on method will these be
included and returned when using
this GetCustomAttributes

//Tony
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

I have one simple question about attribute.
According to the documentation will GetCustomAttributes return all the
custom attributes for this assembly
public virtual Object[] GetCustomAttributes (bool inherit)

I just wonder if I have attribute on classes or on method will these be
included and returned when using
this GetCustomAttributes

//Tony


I don;t think so. The docs says nothing about it.
You can always call the code for yourself and see what you get back :)
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

So what you are actually saying is that this use of GetCustomAttributes will
only
return the attributes for the assembly and not attribut for class and method
and so on.

Correct, you are getting the attributes of the assembly only. Note
that the inherit is ignored (it's clearly stated in the docs)

You will have to iterate in the types defined and then use
Attribute.GetCustomAttributes (Assembly, Type)
 

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