473,657 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1399
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=Thi s is the super cool component
--------------------------------------------------------------------
CustAttr.cpp
Compile with:
cl /clr CustAttr.cpp /link /assemblyresourc e:MyComponent.r esources
--------------------------------------------------------------------
#using <mscorlib.dll >

using namespace System;
using namespace System::Collect ions;
using namespace System::Reflect ion;
using namespace System::Resourc es;

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

public:
DisplayNameAttr ibute(String* resourceName)
: resname_(resour ceName)
{
}

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

//
// Component using the attribute
//
[ DisplayName(S"M yComponent") ]
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(Assembl y* assembly)
{
Hashtable* components = new Hashtable();

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

if ( types[i]->IsAbstract )
continue;
DisplayNameAttr ibute* dnattr[] =
dynamic_cast<Di splayNameAttrib ute*[]>(
types[i]->GetCustomAttri butes(__typeof( DisplayNameAttr ibute),
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::GetEx ecutingAssembly ();
Hashtable* components = ComponentFinder ::FindAll(assem bly);
IDictionaryEnum erator* e = components->GetEnumerator( );
while ( e->MoveNext() )
{
Console::WriteL ine(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=Thi s is the super cool component
--------------------------------------------------------------------
CustAttr.cpp
Compile with:
cl /clr CustAttr.cpp /link /assemblyresourc e:MyComponent.r esources
--------------------------------------------------------------------
#using <mscorlib.dll >

using namespace System;
using namespace System::Collect ions;
using namespace System::Reflect ion;
using namespace System::Resourc es;

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

public:
DisplayNameAttr ibute(String* resourceName)
: resname_(resour ceName)
{
}

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

//
// Component using the attribute
//
[ DisplayName(S"M yComponent") ]
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(Assembl y* assembly)
{
Hashtable* components = new Hashtable();

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

if ( types[i]->IsAbstract )
continue;
DisplayNameAttr ibute* dnattr[] =
dynamic_cast<Di splayNameAttrib ute*[]>(
types[i]->GetCustomAttri butes(__typeof( DisplayNameAttr ibute),
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::GetEx ecutingAssembly ();
Hashtable* components = ComponentFinder ::FindAll(assem bly);
IDictionaryEnum erator* e = components->GetEnumerator( );
while ( e->MoveNext() )
{
Console::WriteL ine(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
2474
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
4
1349
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 every time. Is there a way to preserve the initial instantiation? Thanks, Jim
23
3841
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
6
1348
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 ) { array=obj.value;
8
2139
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 cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
1
1543
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> <head> <title>Proforma</title> <meta http-equiv=Content-Type content="text/html; charset=windows-1252">
1
3367
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 http-equiv=Content-Type content="text/html; charset=windows-1252">
2
1688
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 example the other two fields dont need to be filled in. I just put a line of text in saying: Please ensure either Company Postcode, Agency Number or Policy Number is filled in. but this is not really working well as people are still leaving it blank...
2
3084
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 to a resource in the forms assy, how do I get the reference to the Forms assy? Since it's a component there is no FindForm method.... GetCallingAssy doesn't seem to work either... I have to do it from the component so that on instantiation I can...
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.