P
Paul Hadfield
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.DirectoryVersionAttribute), 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);
}
}
}
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.DirectoryVersionAttribute), 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);
}
}
}