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] >Thread-Topic: GetCustomAttribute not working properly. Please help
>thread-index: AcQrkVQq7dpi9OyTTT+Rii3Cz5ghcQ==
>X-WN-Post: microsoft.public.dotnet.vjsharp
>From: "=?Utf-8?B?U2FtaWRoYQ==?=" <anonymous@discussions.microsoft.com>
>Subject: GetCustomAttribute not working properly. Please help
>Date: Mon, 26 Apr 2004 06:21:05 -0700
>Lines: 157
>Message-ID: <37353156-940E-4AA3-8A87-30C522AE01FF@microsoft.com>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="Utf-8"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Content-Class: urn:content-classes:message
>Importance: normal
>Priority: normal
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
>Newsgroups: microsoft.public.dotnet.vjsharp
>Path: cpmsftngxa10.phx.gbl
>Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.vjsharp:5957
>NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
>X-Tomcat-NG: microsoft.public.dotnet.vjsharp
>
>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 line
AuthorsAttribute authAttr =