all groups > vj# > april 2004 >
You're in the

vj#

group:

GetCustomAttribute not working properly. Please help


GetCustomAttribute not working properly. Please help Samidha
4/26/2004 6:21:05 AM
vj#: Hi
GetCustomAttribute method returns the first custom attribute applied to the type, if any custom attributes are applied to the type. But in the follwoing code it retuning null in the following lin
AuthorsAttribute authAttr = ((AuthorsAttribute)(Attribute.GetCustomAttribute(mInfo, AuthorsAttribute.class.ToType())))
Can any one help me out or is it that custom attributes are not supported by J#
Thanks
The actual code is as follow

package HashCodeJS;
import System.*
import System.Reflection.*
import System.Collections.*

// A custom attribute to allow two authors per method
public class AuthorsAttribute extends Attribut

public AuthorsAttribute(String name1, String name2

authorName1 = name1
authorName2 = name2
} //AuthorsAttribut

protected String authorName1
protected String authorName2

/** @property *
public String get_AuthorName1(

return authorName1

/** @property *
public void set_AuthorName1(String value

authorName1 = value

/** @property *
public String get_AuthorName2(

return authorName2

/** @property *
public void set_AuthorName2(String value

authorName2 = value

// Use the hash code of the string objects and xor them together
public int GetHashCode(

return authorName1.GetHashCode() ^ authorName2.GetHashCode()
} //GetHashCod
} //AuthorsAttribut

// Provide the author names for each method of the class
public class TestClas

/** @ attribute Authors("Immanuel Kant", "Lao Tzu") *
public void Method1(

} //Method
/** @ attribute Authors("Jean-Paul Sartre", "Friedrich Nietzsche") *
public void Method2(

} //Method
/** @ attribute Authors("Immanuel Kant", "Lao Tzu") *
public void Method3(

} //Method
/** @ attribute Authors("Jean-Paul Sartre", "Friedrich Nietzsche") *
public void Method4(

} //Method
/** @ attribute Authors("Immanuel Kant", "Friedrich Nietzsche") *
public void Method5(

} //Method
} //TestClas

class DemoClas

public static void main(String[] args

// Get the class type to access its metadata
Type clsType = TestClass.class.ToType()
// Use a hash table to store the hash codes and methods
Hashtable hTable = new Hashtable()
// Iterate through all the methods of the class.
for (int iCtr = 0; iCtr < clsType.GetMethods().length; iCtr++)
MethodInfo mInfo = (MethodInfo)clsType.GetMethods()[iCtr]

// Get the Authors attribute for the method if it exists
AuthorsAttribute authAttr = ((AuthorsAttribute)(Attribute
GetCustomAttribute(mInfo, AuthorsAttribute.class.ToType())))

if (authAttr != null)
// Get the unique hash code for these authors
int hCode = authAttr.GetHashCode()

// See if it's already been added to the hash table
if (hTable.Contains((Int32)hCode))
// If so, retrieve the array using the Item syntax
ArrayList al = ((ArrayList)(hTable.get_Item((Int32)hCode)))

// Add the method to the array list
al.Add(mInfo.get_Name())

// Put the edited array list back into the table
hTable.set_Item((Int32)hCode, al)

// This is the first occurrence of this hash code
else
// Create a new array list
ArrayList al = new ArrayList()

// Add the method name as the first entry
al.Add(mInfo.get_Name())

// Add the new hash code and array list to the table
hTable.Add((Int32)hCode, al)




// Get the hash table's enumerator and iterate through the table
IDictionaryEnumerator dictEnum = hTable.GetEnumerator();

while (dictEnum.MoveNext()) {
// Get the array list for this iteration.
ArrayList al = ((ArrayList)(dictEnum.get_Value()));

// Access each element of the array.
for (int i = 0; i < al.get_Count(); i++) {
// For the first element, retrieve the author names.
if (i == 0) {
// Get the metadata for the method.
MethodInfo mInfo = clsType.GetMethod(((String)(al.
get_Item(i))));

// Retrieve the Authors attribute again.
AuthorsAttribute authAttr = ((AuthorsAttribute)(Attribute.
GetCustomAttribute(mInfo,
AuthorsAttribute.class.ToType())));

// Display the authors names.
if (authAttr != null) {
Console.WriteLine("Authors: {0}, {1}",authAttr.
get_AuthorName1(), authAttr.get_AuthorName2());
}
else {
Console.WriteLine("The authors could not be "
+ "retrieved.");
}
}
Console.WriteLine(al.get_Item(i));
}
Console.WriteLine("");
}
} //main
RE: GetCustomAttribute not working properly. Please help diganta.vjcr NO[at]SPAM online.microsoft.com
9/3/2004 10:13:15 AM
You actually have attached the attribute as "/** @ property*/", whereas the
compiler expects it to be "/**@property*/" - notice there is no space
between '@' and 'property'.

Also another change you need to make, is to attach "/**@attribute
AttributeUsage(AttributeTargets.All)*/" to the attribute definition of
'AuthorsAttribute'.

Here's the modified code.

package HashCodeJS;
import System.*;
import System.Reflection.*;
import System.Collections.*;

// A custom attribute to allow two authors per method.
/**@attribute AttributeUsage(AttributeTargets.All)*/
public class AuthorsAttribute extends Attribute
{
public AuthorsAttribute(String name1, String name2)
{
authorName1 = name1;
authorName2 = name2;
} //AuthorsAttribute

protected String authorName1;
protected String authorName2;

/** @property */
public String get_AuthorName1()
{
return authorName1;
}
/** @property */
public void set_AuthorName1(String value)
{
authorName1 = value;
}
/** @property */
public String get_AuthorName2()
{
return authorName2;
}
/** @property */
public void set_AuthorName2(String value)
{
authorName2 = value;
}
// Use the hash code of the string objects and xor them together.
public int GetHashCode()
{
return authorName1.GetHashCode() ^ authorName2.GetHashCode();
} //GetHashCode
} //AuthorsAttribute

// Provide the author names for each method of the class.
public class TestClass
{
/** @attribute Authors("Immanuel Kant", "Lao Tzu") */
public void Method1()
{
} //Method1
/** @attribute Authors("Jean-Paul Sartre", "Friedrich Nietzsche") */
public void Method2()
{
} //Method2
/** @attribute Authors("Immanuel Kant", "Lao Tzu") */
public void Method3()
{
} //Method3
/** @attribute Authors("Jean-Paul Sartre", "Friedrich Nietzsche") */
public void Method4()
{
} //Method4
/** @attribute Authors("Immanuel Kant", "Friedrich Nietzsche") */
public void Method5()
{
} //Method5
} //TestClass

class DemoClass
{
public static void main(String[] args)
{
// Get the class type to access its metadata.
Type clsType = TestClass.class.ToType();
// Use a hash table to store the hash codes and methods.
Hashtable hTable = new Hashtable();
// Iterate through all the methods of the class.
for (int iCtr = 0; iCtr < clsType.GetMethods().length; iCtr++) {
MethodInfo mInfo = (MethodInfo)clsType.GetMethods()[iCtr];

// Get the Authors attribute for the method if it exists.
AuthorsAttribute authAttr = ((AuthorsAttribute)(Attribute.
GetCustomAttribute(mInfo,
AuthorsAttribute.class.ToType())));

if (authAttr != null) {
// Get the unique hash code for these authors.
int hCode = authAttr.GetHashCode();

// See if it's already been added to the hash table.
if (hTable.Contains((Int32)hCode)) {
// If so, retrieve the array using the Item syntax.
ArrayList al =
((ArrayList)(hTable.get_Item((Int32)hCode)));

// Add the method to the array list.
al.Add(mInfo.get_Name());

// Put the edited array list back into the table.
hTable.set_Item((Int32)hCode, al);
}
// This is the first occurrence of this hash code.
else {
// Create a new array list.
ArrayList al = new ArrayList();

// Add the method name as the first entry.
al.Add(mInfo.get_Name());

// Add the new hash code and array list to the table.
hTable.Add((Int32)hCode, al);
}
}
}

// Get the hash table's enumerator and iterate through the table.
IDictionaryEnumerator dictEnum = hTable.GetEnumerator();

while (dictEnum.MoveNext()) {
// Get the array list for this iteration.
ArrayList al = ((ArrayList)(dictEnum.get_Value()));

// Access each element of the array.
for (int i = 0; i < al.get_Count(); i++) {
// For the first element, retrieve the author names.
if (i == 0) {
// Get the metadata for the method.
MethodInfo mInfo = clsType.GetMethod(((String)(al.
get_Item(i))));

// Retrieve the Authors attribute again.
AuthorsAttribute authAttr =
((AuthorsAttribute)(Attribute.
GetCustomAttribute(mInfo,
AuthorsAttribute.class.ToType())));

// Display the authors names.
if (authAttr != null) {
Console.WriteLine("Authors: {0}, {1}",authAttr.
get_AuthorName1(), authAttr.get_AuthorName2());
}
else {
Console.WriteLine("The authors could not be "
+ "retrieved.");
}
}
Console.WriteLine(al.get_Item(i));
}
Console.WriteLine("");
}
} //main
} //DemoClass


Thanks,
Diganta Roy.
Microsoft J# .NET Product Team.
--------------------
[quoted text, click to view]
GetCustomAttribute method returns the first custom attribute applied
to the type, if any custom attributes are applied to the type. But in the
follwoing code it retuning null in the following line
AuthorsAttribute authAttr =
AddThis Social Bookmark Button