Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso.gastaldi@xxxxxxxxxxx
- Date: 31 Jul 2006 05:04:38 -0700
hi Jeff,
Couldn't you, in case of error, just create a "dummy" instance of the
derived class,
decorated as you wish?
In this case you could also change the attributes depending on the
exception type.
Do you think that could make sense?
-tom
Jeff Mason ha scritto:
On 31 Jul 2006 01:54:21 -0700, tommaso.gastaldi@xxxxxxxxxxx wrote:
Hi Jeff,
if the problem were just to obtain the type you could use
GetType(YourBaseClass) ...
I think I tried GetType(YourBaseClass), but it returned the type of the Base class
instead of the derived class. The derived class is where the attribute is located.
but I am a little perplexed as it does not seem to make sense to have
attributes detached from an instance.
Can you make an example where this can be meaningful. And why you need
to use attributes to do that ?
I was hoping to avoid the "why do you need to do that" sorts of questions, since they
can be distracting from the problem at hand, but OK.
The attribute I'd like to define is a "Friendly Name" attribute. This name will be
used in, for example, error messages involving the class. I may define a class
called, say, "EntLOB", but I want to use a more friendly name, like, "Enterprise Line
of Business" when presenting errors involving that class.
Many times the "Friendly Name" is the same as the class name (like "Customer"), but
in more than a few cases the class name I may use as a programmer is not the best
choice as a name for presentation to an end user.
It's true that almost all error messages would involve an instance of the class, but
consider the case where an instance of the object cannot be created at all. Perhaps
the user has entered some data (e.g. search criteria) which prevents an instance from
being created. The network could be down. The database could be offline. I'd like
the error message to be something like:
Unable to retrieve the Enterprise Line of Business because ...
rather than
Unable to retrieve the EntLOB because ...
The former is more, er, friendly, than the latter.
Since the database went offline, the object could not be constructed and there is no
instance of the EntLOB class available to get the type of. Since all the objects that
I may create derive from a common base class, I'd like to decorate my derived classes
with this attribute as appropriate, and have a common method, implemented in the base
class, which accesses this attribute and returns the specified name from the
attribute.
Since I may need to access this property even if no specific instance of the derived
class exists, I want to define a shared method in the base class which returns the
friendly name of the derived class (i.e. rather than the name of an instance), which
is where the attribute is located. I was hoping that I could use syntax like:
errormsg = string.format("Unable to retrieve {0} ...", EntLOB.FriendlyName, ...) ...
where FriendlyName is a shared readonly property implemented in the base class.
HTH,
-- Jeff
.
- Follow-Ups:
- References:
- Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason
- Custom Attributes, Shared methods, Derived classes and reflection
- Prev by Date: Re: Week Ending
- Next by Date: Threads
- Previous by thread: Re: Custom Attributes, Shared methods, Derived classes and reflection
- Next by thread: Re: Custom Attributes, Shared methods, Derived classes and reflection
- Index(es):
Relevant Pages
|