Mandatory component class instantiation ...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

We have developed a few .NET components. The application access these
components through well defined interface IOurInterface.

The application list display name of each component in the dialog, so
user can choose which one he wants to use. To display list of available
components we have to associate human redable display name with each
component.

To get that display name of component we added the method GetDisplayName to
IOurInterface.

The implication is that we have to instantiate class that implements
IOurInterface
in all components, so we can call GetDisplayName method of each instance.
We don't know other ways to get display name of component. The static method
would be great but we can't add static method to interface IOurInterface.

Can anyone suggest the better way to list human redable display name of
components without instantiating all components. Thanks.
 
Dave,
We have developed a few .NET components. The application access these
components through well defined interface IOurInterface.

The application list display name of each component in the dialog, so
user can choose which one he wants to use. To display list of available
components we have to associate human redable display name with each
component.

To get that display name of component we added the method GetDisplayName to
IOurInterface.

The implication is that we have to instantiate class that implements
IOurInterface
in all components, so we can call GetDisplayName method of each instance.
We don't know other ways to get display name of component. The static method
would be great but we can't add static method to interface IOurInterface.

Can anyone suggest the better way to list human redable display name of
components without instantiating all components. Thanks.

How about querying for a custom attribute on the type using reflection?
Works wonders for all the VS.NET designers and quite a few other things...
 
Well, we did considered that option, but how would you localize the custom
attribute for different languages. The display name of the component
obviously will be different in Spanish or German, but you can't localize
custom attribute. Am I correct? Could you comment.
 
Hi Dave,
Well, we did considered that option, but how would you localize the custom
attribute for different languages. The display name of the component
obviously will be different in Spanish or German, but you can't localize
custom attribute. Am I correct? Could you comment.

It wouldn't be a problem, really. Have the custom attribute receive a
resource identifier instead of the actual text, and then have on it a method
that retrieves it using ResourceManager.

Then, your code just finds the attribute, and calls the method, and voila.
 
Hi Tomas,

Thanks for your advice. Sounds interesting. We definitely missed out that we
can store string ID as an attribute, instead of string itself.

I have another question, can we add custom attribute to the Type class,
instead of assembly itself? What I basically want is attach string ID
attribute to the Type and get Display Name of the type without first
instantiating it. There is a TypeAttributes object "Attributes" representing
the attribute set of the Type. But it doesn't seem to me much of help.

I understand that I can attach custom attribute to the assembly itself, but
in this case we will have one display name per assembly. We might implement
several types in one assembly and it would require multiple Display Names per
assembly.
In this case it would be useful if we can attach string ID to type instead
of assembly.

Thanks.

Dave
 
Hi Dave,
Thanks for your advice. Sounds interesting. We definitely missed out that we
can store string ID as an attribute, instead of string itself.

I have another question, can we add custom attribute to the Type class,
instead of assembly itself?

Sure, that would be the usual way to do it.
What I basically want is attach string ID
attribute to the Type and get Display Name of the type without first
instantiating it. There is a TypeAttributes object "Attributes" representing
the attribute set of the Type. But it doesn't seem to me much of help.

It's really not hard to use custom attributes. Consider this simple example
that basically implements something like what you want (minus some error
checks):

--------------------------------------------------------------------
MyComponent.txt.
Compile with: resgen MyComponent.txt
--------------------------------------------------------------------
MyComponent=This is the super cool component


--------------------------------------------------------------------
CustAttr.cpp
Compile with:
cl /clr CustAttr.cpp /link /assemblyresource:MyComponent.resources
--------------------------------------------------------------------
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Reflection;
using namespace System::Resources;

//
// Our custom attribute
//
public __gc class DisplayNameAttribute : public Attribute
{
private:
String* resname_;

public:
DisplayNameAttribute(String* resourceName)
: resname_(resourceName)
{
}

String* GetDisplayName(Type* type) {
ResourceManager* mgr = new ResourceManager(type);
return mgr->GetString(resname_);
}
};

//
// Component using the attribute
//
[ DisplayName(S"MyComponent") ]
public __gc class MyComponent
{
// ...
};

//
// Class used to find components in a
// given assembly. Returns a Hashtable
// with the component display name
// and the type implementing it
//
public __gc class ComponentFinder
{
public:
static Hashtable* FindAll(Assembly* assembly)
{
Hashtable* components = new Hashtable();

Type* types[] = assembly->GetTypes();
for ( int i=0; i < types->Length; i++ )
{

if ( types->IsAbstract )
continue;
DisplayNameAttribute* dnattr[] =
dynamic_cast<DisplayNameAttribute*[]>(
types->GetCustomAttributes(__typeof(DisplayNameAttribute),
false)
);

if ( dnattr != 0 && dnattr->Length > 0 ) {
components->Add(dnattr[0]->GetDisplayName(types), types);
}
}
return components;
}
};


//
// get list of components in current
// assembly and print them
//
int main()
{
Assembly* assembly = Assembly::GetExecutingAssembly();
Hashtable* components = ComponentFinder::FindAll(assembly);
IDictionaryEnumerator* e = components->GetEnumerator();
while ( e->MoveNext() )
{
Console::WriteLine(S"{0} - {1}", e->Key, e->Value);
}
}
 
Thank you Tomas

Tomas Restrepo (MVP) said:
Hi Dave,
Thanks for your advice. Sounds interesting. We definitely missed out that we
can store string ID as an attribute, instead of string itself.

I have another question, can we add custom attribute to the Type class,
instead of assembly itself?

Sure, that would be the usual way to do it.
What I basically want is attach string ID
attribute to the Type and get Display Name of the type without first
instantiating it. There is a TypeAttributes object "Attributes" representing
the attribute set of the Type. But it doesn't seem to me much of help.

It's really not hard to use custom attributes. Consider this simple example
that basically implements something like what you want (minus some error
checks):

--------------------------------------------------------------------
MyComponent.txt.
Compile with: resgen MyComponent.txt
--------------------------------------------------------------------
MyComponent=This is the super cool component


--------------------------------------------------------------------
CustAttr.cpp
Compile with:
cl /clr CustAttr.cpp /link /assemblyresource:MyComponent.resources
--------------------------------------------------------------------
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Reflection;
using namespace System::Resources;

//
// Our custom attribute
//
public __gc class DisplayNameAttribute : public Attribute
{
private:
String* resname_;

public:
DisplayNameAttribute(String* resourceName)
: resname_(resourceName)
{
}

String* GetDisplayName(Type* type) {
ResourceManager* mgr = new ResourceManager(type);
return mgr->GetString(resname_);
}
};

//
// Component using the attribute
//
[ DisplayName(S"MyComponent") ]
public __gc class MyComponent
{
// ...
};

//
// Class used to find components in a
// given assembly. Returns a Hashtable
// with the component display name
// and the type implementing it
//
public __gc class ComponentFinder
{
public:
static Hashtable* FindAll(Assembly* assembly)
{
Hashtable* components = new Hashtable();

Type* types[] = assembly->GetTypes();
for ( int i=0; i < types->Length; i++ )
{

if ( types->IsAbstract )
continue;
DisplayNameAttribute* dnattr[] =
dynamic_cast<DisplayNameAttribute*[]>(
types->GetCustomAttributes(__typeof(DisplayNameAttribute),
false)
);

if ( dnattr != 0 && dnattr->Length > 0 ) {
components->Add(dnattr[0]->GetDisplayName(types), types);
}
}
return components;
}
};


//
// get list of components in current
// assembly and print them
//
int main()
{
Assembly* assembly = Assembly::GetExecutingAssembly();
Hashtable* components = ComponentFinder::FindAll(assembly);
IDictionaryEnumerator* e = components->GetEnumerator();
while ( e->MoveNext() )
{
Console::WriteLine(S"{0} - {1}", e->Key, e->Value);
}
}
 
Back
Top