473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Type.GetCustomerAttributes(...) Not Working as expected

Hi,

I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system "Custom Attributes" by type. The output below identifies that I can catch the "DefaultMemberAttribute", but I can not catch my own "DirectoryVersionAttribute". Cany anyone throw any light on this as it is driving mad

Thanks in Advance,

- Paul.

------------------------------------------------
System.Reflection.DefaultMemberAttribute
System.Reflection.DefaultMemberAttribute
True
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
MyNameSpace.DirectoryVersionAttribute
MyNameSpace.DirectoryVersionAttribute
False
Press any key to continue . . .
------------------------------------------------

using System;
namespace MyNameSpace
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class DirectoryVersionAttribute : System.Attribute
{
private string version = null;
public DirectoryVersionAttribute(string version)
{
this.version = version;
}
public string Version { get { return this.version; } }
}
}

using MyNameSpace;
namespace AnotherNameSpace
{
[DirectoryVersion("1.00")]
public class DirectoryBuilder_v1_00 : IDirectoryBuilder
{
...
}

private void GetBuilderFile(string fileName)
{
Assembly assembly = Assembly.LoadFrom(fileName);
foreach (Type type in assembly.GetTypes())
{
object[] test = type.GetCustomAttributes(typeof(MyNameSpace.Direct oryVersionAttribute), false);
if (test.Length 0)
{
Console.WriteLine("Wishful Thinking!"); // <<-- Never gets called!
}
test = type.GetCustomAttributes(typeof(Attribute), false);
if (test.Length 0)
{
Console.WriteLine(((Attribute)test[0]).TypeId);
Console.WriteLine(((Attribute)test[0]).ToString());
Console.WriteLine(((Attribute)test[0]) is System.Reflection.DefaultMemberAttribute);
Console.WriteLine(((Attribute)test[0]) is MyNameSpace.DirectoryVersionAttribute);
}
}
}
May 25 '07 #1
7 2341
Paul,

I'm not sure on this as I've only messed with reflection a little but
wouldn't one get the class with the attribute on it first then get the custom
attributes for it? I know this is what I did for custom attributes I put on
some properties one time. Hope this helps.
--
Thom
"Paul Hadfield" wrote:
Hi,

I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system "Custom Attributes" by type. The output below identifies that I can catch the "DefaultMemberAttribute", but I can not catch my own "DirectoryVersionAttribute". Cany anyone throw any light on this as it is driving mad

Thanks in Advance,

- Paul.

------------------------------------------------
System.Reflection.DefaultMemberAttribute
System.Reflection.DefaultMemberAttribute
True
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
MyNameSpace.DirectoryVersionAttribute
MyNameSpace.DirectoryVersionAttribute
False
Press any key to continue . . .
------------------------------------------------

using System;
namespace MyNameSpace
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class DirectoryVersionAttribute : System.Attribute
{
private string version = null;
public DirectoryVersionAttribute(string version)
{
this.version = version;
}
public string Version { get { return this.version; } }
}
}

using MyNameSpace;
namespace AnotherNameSpace
{
[DirectoryVersion("1.00")]
public class DirectoryBuilder_v1_00 : IDirectoryBuilder
{
...
}

private void GetBuilderFile(string fileName)
{
Assembly assembly = Assembly.LoadFrom(fileName);
foreach (Type type in assembly.GetTypes())
{
object[] test = type.GetCustomAttributes(typeof(MyNameSpace.Direct oryVersionAttribute), false);
if (test.Length 0)
{
Console.WriteLine("Wishful Thinking!"); // <<-- Never gets called!
}
test = type.GetCustomAttributes(typeof(Attribute), false);
if (test.Length 0)
{
Console.WriteLine(((Attribute)test[0]).TypeId);
Console.WriteLine(((Attribute)test[0]).ToString());
Console.WriteLine(((Attribute)test[0]) is System.Reflection.DefaultMemberAttribute);
Console.WriteLine(((Attribute)test[0]) is MyNameSpace.DirectoryVersionAttribute);
}
}
}
May 25 '07 #2
Hi Thom,

Whilst this is a code snippet, I have run the full code through the debugger
and the snippet illustrates the area that has the issue. The statement
"foreach (Type type in assembly.GetTypes())" gets a list of the classes that
are defined in that assembly. The output from "test =
type.GetCustomAttributes(typeof(Attribute), false);" shows that the custom
attribute can be retrieved from the class, I'm just not sure why the
GetCustomAttribute using the proper type doesn't return anything.

BTW:

"test = type.GetCustomAttributes(typeof(DefaultMemberAttri bute), false);"

returns one element in the array, which is the attribute that has been
defined - it's just why doesn't it work with my custom attributes - what am
I doing wrong / differently?

- Paul.

"tbain" <tb***@discussions.microsoft.comwrote in message
news:88**********************************@microsof t.com...
Paul,

I'm not sure on this as I've only messed with reflection a little but
wouldn't one get the class with the attribute on it first then get the
custom
attributes for it? I know this is what I did for custom attributes I put
on
some properties one time. Hope this helps.
--
Thom
"Paul Hadfield" wrote:
>Hi,

I'm running into one problem with trying to call
"Type.GetCustomAttributes(...)" on my reflected code. Basically - I
can't trap my own custom attribute - I can only catch / identify system
"Custom Attributes" by type. The output below identifies that I can
catch the "DefaultMemberAttribute", but I can not catch my own
"DirectoryVersionAttribute". Cany anyone throw any light on this as it
is driving mad

Thanks in Advance,

- Paul.

------------------------------------------------
System.Reflection.DefaultMemberAttribute
System.Reflection.DefaultMemberAttribute
True
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
MyNameSpace.DirectoryVersionAttribute
MyNameSpace.DirectoryVersionAttribute
False
Press any key to continue . . .
------------------------------------------------

using System;
namespace MyNameSpace
{
[AttributeUsage(AttributeTargets.Class, Inherited = false,
AllowMultiple = false)]
public class DirectoryVersionAttribute : System.Attribute
{
private string version = null;
public DirectoryVersionAttribute(string version)
{
this.version = version;
}
public string Version { get { return this.version; } }
}
}

using MyNameSpace;
namespace AnotherNameSpace
{
[DirectoryVersion("1.00")]
public class DirectoryBuilder_v1_00 : IDirectoryBuilder
{
...
}

private void GetBuilderFile(string fileName)
{
Assembly assembly = Assembly.LoadFrom(fileName);
foreach (Type type in assembly.GetTypes())
{
object[] test =
type.GetCustomAttributes(typeof(MyNameSpace.Direc toryVersionAttribute),
false);
if (test.Length 0)
{
Console.WriteLine("Wishful Thinking!"); // <<-- Never gets
called!
}
test = type.GetCustomAttributes(typeof(Attribute), false);
if (test.Length 0)
{
Console.WriteLine(((Attribute)test[0]).TypeId);
Console.WriteLine(((Attribute)test[0]).ToString());
Console.WriteLine(((Attribute)test[0]) is
System.Reflection.DefaultMemberAttribute);
Console.WriteLine(((Attribute)test[0]) is
MyNameSpace.DirectoryVersionAttribute);
}
}
}

May 25 '07 #3
* tbain wrote, On 25-5-2007 13:27:
Paul,

I'm not sure on this as I've only messed with reflection a little but
wouldn't one get the class with the attribute on it first then get the custom
attributes for it? I know this is what I did for custom attributes I put on
some properties one time. Hope this helps.
In which Assembly is DirectoryVersion defined. Is it loaded in your test
app?

You might want to load it with Assembly.LoadFrom is this isn't so and
try again.

Jesse
May 25 '07 #4
As shown in original code, I'm using Assembly.LoadFrom() as that's the
reflection bit.

The custom attribute DirectoryVersion is defined in the same assembly as the
code checking for the custom attributes. However in my testing I've moved
the code checking for the customer attribute into another project too -
there was no change in behaviour. The class that has custom attribute
assigned is in a separate assembly (which has a reference to the assembly to
get the definition).

I'm willing to investigate any possibility right now as the difference must
be in my code, but I can't for the life of me see it, and I believe that
I've tried most assembly combinations. I've also tried typeof(...) and
Type.GetType(...)

- Paul.

"Jesse Houwing" <je***********@nospam-sogeti.nlwrote in message
news:e6**************@TK2MSFTNGP06.phx.gbl...
>* tbain wrote, On 25-5-2007 13:27:
>Paul,

I'm not sure on this as I've only messed with reflection a little but
wouldn't one get the class with the attribute on it first then get the
custom attributes for it? I know this is what I did for custom attributes
I put on some properties one time. Hope this helps.

In which Assembly is DirectoryVersion defined. Is it loaded in your test
app?

You might want to load it with Assembly.LoadFrom is this isn't so and try
again.

Jesse

May 25 '07 #5
Not sure if this might help diagnose the problem, but the code snippet below throws the exception

----------------------------------------------
Unable to cast object of type 'MyNameSpace.DirectoryVersionAttribute' to type 'MyNameSpace.DirectoryVersionAttribute'.
----------------------------------------------

Would cause two apparently similar object types throw an exception on casting.

----------------------------------------------
foreach (Type type in assembly.GetTypes())
{
object[] attributes = type.GetCustomAttributes(false);
foreach (object attribute in attributes)
{
if (attribute.GetType().Name == "DirectoryVersionAttribute")
{
Console.WriteLine("Directory Version: {0}", ((DirectoryVersionAttribute)attribute).Version); // <- Throws Exception here
}
}
}
"Paul Hadfield" <no****@noone.comwrote in message news:OW**************@TK2MSFTNGP02.phx.gbl...
Hi,

I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system "Custom Attributes" by type. The output below identifies that I can catch the "DefaultMemberAttribute", but I can not catch my own "DirectoryVersionAttribute". Cany anyone throw any light on this as it is driving mad

Thanks in Advance,

- Paul.

------------------------------------------------
System.Reflection.DefaultMemberAttribute
System.Reflection.DefaultMemberAttribute
True
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
MyNameSpace.DirectoryVersionAttribute
MyNameSpace.DirectoryVersionAttribute
False
Press any key to continue . . .
------------------------------------------------

using System;
namespace MyNameSpace
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class DirectoryVersionAttribute : System.Attribute
{
private string version = null;
public DirectoryVersionAttribute(string version)
{
this.version = version;
}
public string Version { get { return this.version; } }
}
}

using MyNameSpace;
namespace AnotherNameSpace
{
[DirectoryVersion("1.00")]
public class DirectoryBuilder_v1_00 : IDirectoryBuilder
{
...
}

private void GetBuilderFile(string fileName)
{
Assembly assembly = Assembly.LoadFrom(fileName);
foreach (Type type in assembly.GetTypes())
{
object[] test = type.GetCustomAttributes(typeof(MyNameSpace.Direct oryVersionAttribute), false);
if (test.Length 0)
{
Console.WriteLine("Wishful Thinking!"); // <<-- Never gets called!
}
test = type.GetCustomAttributes(typeof(Attribute), false);
if (test.Length 0)
{
Console.WriteLine(((Attribute)test[0]).TypeId);
Console.WriteLine(((Attribute)test[0]).ToString());
Console.WriteLine(((Attribute)test[0]) is System.Reflection.DefaultMemberAttribute);
Console.WriteLine(((Attribute)test[0]) is MyNameSpace.DirectoryVersionAttribute);
}
}
}
May 25 '07 #6
I've found the cause, but not sure on what is needed for a clean solution! In my solution I have multiple projects, each compiling into their own DLL's. The console application test harness code, which checks for the occurance of the Custom Attribute is in one class has a reference to the project that contains the attribute (these end up in one directory). The code that uses the custom attribute resides in another project, also with a reference to the project that contains the attribute (these end up in another directory). So the DLL containing the attribute definition is local to each directory - hence what I believe to be the clash.

So this does not work (note, DirectoryVersionAttribute.dll is the same dll, just in two places)

\Dir1\
Test.Exe
DirectoryVersionAttribute.dll
\Dir2\
DirectoryLoader.dll
DirectoryVersionAttribute.dll

However the following (same exe / dll's, just copied) works fine

\Dir3\
Test.Exe
DirectoryLoader.dll
DirectoryVersionAttribute.dll

What is the best solution to this problem, should I start looking into using the GAC ?
"Paul Hadfield" <no****@noone.comwrote in message news:eB**************@TK2MSFTNGP02.phx.gbl...
Not sure if this might help diagnose the problem, but the code snippet below throws the exception

----------------------------------------------
Unable to cast object of type 'MyNameSpace.DirectoryVersionAttribute' to type 'MyNameSpace.DirectoryVersionAttribute'.
----------------------------------------------

Would cause two apparently similar object types throw an exception on casting.

----------------------------------------------
foreach (Type type in assembly.GetTypes())
{
object[] attributes = type.GetCustomAttributes(false);
foreach (object attribute in attributes)
{
if (attribute.GetType().Name == "DirectoryVersionAttribute")
{
Console.WriteLine("Directory Version: {0}", ((DirectoryVersionAttribute)attribute).Version); // <- Throws Exception here
}
}
}
"Paul Hadfield" <no****@noone.comwrote in message news:OW**************@TK2MSFTNGP02.phx.gbl...
Hi,

I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system "Custom Attributes" by type. The output below identifies that I can catch the "DefaultMemberAttribute", but I can not catch my own "DirectoryVersionAttribute". Cany anyone throw any light on this as it is driving mad

Thanks in Advance,

- Paul.

------------------------------------------------
System.Reflection.DefaultMemberAttribute
System.Reflection.DefaultMemberAttribute
True
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
MyNameSpace.DirectoryVersionAttribute
MyNameSpace.DirectoryVersionAttribute
False
Press any key to continue . . .
------------------------------------------------

using System;
namespace MyNameSpace
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class DirectoryVersionAttribute : System.Attribute
{
private string version = null;
public DirectoryVersionAttribute(string version)
{
this.version = version;
}
public string Version { get { return this.version; } }
}
}

using MyNameSpace;
namespace AnotherNameSpace
{
[DirectoryVersion("1.00")]
public class DirectoryBuilder_v1_00 : IDirectoryBuilder
{
...
}

private void GetBuilderFile(string fileName)
{
Assembly assembly = Assembly.LoadFrom(fileName);
foreach (Type type in assembly.GetTypes())
{
object[] test = type.GetCustomAttributes(typeof(MyNameSpace.Direct oryVersionAttribute), false);
if (test.Length 0)
{
Console.WriteLine("Wishful Thinking!"); // <<-- Never gets called!
}
test = type.GetCustomAttributes(typeof(Attribute), false);
if (test.Length 0)
{
Console.WriteLine(((Attribute)test[0]).TypeId);
Console.WriteLine(((Attribute)test[0]).ToString());
Console.WriteLine(((Attribute)test[0]) is System.Reflection.DefaultMemberAttribute);
Console.WriteLine(((Attribute)test[0]) is MyNameSpace.DirectoryVersionAttribute);
}
}
}
May 25 '07 #7
Could you try to use reflection on the attribute to get its properties,
"Version" in this case, then use GetValue to fetch it? Basically, use more
reflection instead of casting. I just looked at my old code and that's how I
retrieved values of the custom attribute. Again, hope this helps.
--
Thom
"Paul Hadfield" wrote:
I've found the cause, but not sure on what is needed for a clean solution! In my solution I have multiple projects, each compiling into their own DLL's. The console application test harness code, which checks for the occurance of the Custom Attribute is in one class has a reference to the project that contains the attribute (these end up in one directory). The code that uses the custom attribute resides in another project, also with a reference to the project that contains the attribute (these end up in another directory). So the DLL containing the attribute definition is local to each directory - hence what I believe to be the clash.

So this does not work (note, DirectoryVersionAttribute.dll is the same dll, just in two places)

\Dir1\
Test.Exe
DirectoryVersionAttribute.dll
\Dir2\
DirectoryLoader.dll
DirectoryVersionAttribute.dll

However the following (same exe / dll's, just copied) works fine

\Dir3\
Test.Exe
DirectoryLoader.dll
DirectoryVersionAttribute.dll

What is the best solution to this problem, should I start looking into using the GAC ?
"Paul Hadfield" <no****@noone.comwrote in message news:eB**************@TK2MSFTNGP02.phx.gbl...
Not sure if this might help diagnose the problem, but the code snippet below throws the exception

----------------------------------------------
Unable to cast object of type 'MyNameSpace.DirectoryVersionAttribute' to type 'MyNameSpace.DirectoryVersionAttribute'.
----------------------------------------------

Would cause two apparently similar object types throw an exception on casting.

----------------------------------------------
foreach (Type type in assembly.GetTypes())
{
object[] attributes = type.GetCustomAttributes(false);
foreach (object attribute in attributes)
{
if (attribute.GetType().Name == "DirectoryVersionAttribute")
{
Console.WriteLine("Directory Version: {0}", ((DirectoryVersionAttribute)attribute).Version); // <- Throws Exception here
}
}
}
"Paul Hadfield" <no****@noone.comwrote in message news:OW**************@TK2MSFTNGP02.phx.gbl...
Hi,

I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system "Custom Attributes" by type. The output below identifies that I can catch the "DefaultMemberAttribute", but I can not catch my own "DirectoryVersionAttribute". Cany anyone throw any light on this as it is driving mad

Thanks in Advance,

- Paul.

------------------------------------------------
System.Reflection.DefaultMemberAttribute
System.Reflection.DefaultMemberAttribute
True
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
System.AttributeUsageAttribute
System.AttributeUsageAttribute
False
MyNameSpace.DirectoryVersionAttribute
MyNameSpace.DirectoryVersionAttribute
False
Press any key to continue . . .
------------------------------------------------

using System;
namespace MyNameSpace
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class DirectoryVersionAttribute : System.Attribute
{
private string version = null;
public DirectoryVersionAttribute(string version)
{
this.version = version;
}
public string Version { get { return this.version; } }
}
}

using MyNameSpace;
namespace AnotherNameSpace
{
[DirectoryVersion("1.00")]
public class DirectoryBuilder_v1_00 : IDirectoryBuilder
{
...
}

private void GetBuilderFile(string fileName)
{
Assembly assembly = Assembly.LoadFrom(fileName);
foreach (Type type in assembly.GetTypes())
{
object[] test = type.GetCustomAttributes(typeof(MyNameSpace.Direct oryVersionAttribute), false);
if (test.Length 0)
{
Console.WriteLine("Wishful Thinking!"); // <<-- Never gets called!
}
test = type.GetCustomAttributes(typeof(Attribute), false);
if (test.Length 0)
{
Console.WriteLine(((Attribute)test[0]).TypeId);
Console.WriteLine(((Attribute)test[0]).ToString());
Console.WriteLine(((Attribute)test[0]) is System.Reflection.DefaultMemberAttribute);
Console.WriteLine(((Attribute)test[0]) is MyNameSpace.DirectoryVersionAttribute);
}
}
}
May 25 '07 #8

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

Similar topics

2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
9
by: christer-sandberg | last post by:
When I typecast a function and call it trough the casted pointer (se code below) I get the warnings: warning: function called through a non-compatible type if this code is reached, the program...
2
by: Dave Leach | last post by:
I am writing a Windows MDI application in C#. I would like to cast an object to a specific type. I know that the object is of a compatible type. The code below shows what I would like to do, but...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
1
by: The Late Nate the Great | last post by:
Using VC# in creating a .NET Compact Framework project, I received this message first thing Monday morning (talk about a bad omen for the week!). It gave a line number that didn't apply, and...
5
by: Alfredo Magallón Arbizu | last post by:
Hi, I have an ASP.NET app that works perfectly in Windows Server 2003, but fails in Windows 2000. It fails when trying to read the data in an Excel Workbook (range.value)... The error is: ...
3
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
14
by: Geoff Jones | last post by:
Hi I'm trying to use a class that I've written in a form. Unfortunately, when I write something like: Public x As New myClass I get the compile time error: "Type Expected". Indeed, the...
1
by: zoneal | last post by:
Hi I am trying to get html response from a web server for a given URL . I have written following code. Imports System.Net Imports System.IO Dim objRequest As HTTPWebRequest =...
4
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
I am getting this error with a switch statement: what doe it mean? int res = new int {Convert.ToInt32(arg1 is string), Convert.ToInt32(arg2 is string)}; switch (res) { case new int{0,0}: {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.