473,320 Members | 1,952 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,320 software developers and data experts.

Mandatory component class instantiation ...

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.
Nov 17 '05 #1
6 1385
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...

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2
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.

"Tomas Restrepo (MVP)" wrote:
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...

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #3
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.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4
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

"Tomas Restrepo (MVP)" wrote:
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.

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #5
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[i]->IsAbstract )
continue;
DisplayNameAttribute* dnattr[] =
dynamic_cast<DisplayNameAttribute*[]>(
types[i]->GetCustomAttributes(__typeof(DisplayNameAttribute ),
false)
);

if ( dnattr != 0 && dnattr->Length > 0 ) {
components->Add(dnattr[0]->GetDisplayName(types[i]), types[i]);
}
}
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);
}
}

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #6
Thank you Tomas

"Tomas Restrepo (MVP)" wrote:
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[i]->IsAbstract )
continue;
DisplayNameAttribute* dnattr[] =
dynamic_cast<DisplayNameAttribute*[]>(
types[i]->GetCustomAttributes(__typeof(DisplayNameAttribute ),
false)
);

if ( dnattr != 0 && dnattr->Length > 0 ) {
components->Add(dnattr[0]->GetDisplayName(types[i]), types[i]);
}
}
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);
}
}

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #7

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

Similar topics

7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Jim Hammond | last post by:
Every time I call a method in a client-side assembly (component), it seems like the component gets instantiated again. More specifically, I have determined that the Init method is getting called...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
6
by: Ruso | last post by:
I have this function, which should change a value of the texfield(s) depending on the radio buttons selection. function foo(obj) { if (!array) { flag++; } if (array != obj.value ) {...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
1
by: AR123 | last post by:
Hi I have set up mandatory form fields but it dosne t seem to be working. Would appreciate it if somone could have a look. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> ...
1
by: AR123 | last post by:
The mandatory form field that is not working is the TYPE OF ILLUSTRATION. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Proforma</title> <meta...
2
by: AR123 | last post by:
Hi I have set up a form. What I want to to is with the fields: Company Postcode Agency Number Policy Number I want these to be mandatory however if someone fills in the company postcode for...
2
by: =?Utf-8?B?cnlhbmg=?= | last post by:
I've created a class library of controls/components. For the component i implemented a custom designer that tracks the custom controls added to a form. If i wanted to serialize the component out...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.