Re: Custom Attributes, Shared methods, Derived classes and reflection



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

.



Relevant Pages

  • Re: Application.Run() problem
    ... still am very much confused about the paint event handler. ... It most obviously should be the derived class, ... It seems that you're not understanding how inheritance works. ... which ensures that the base class uses the same ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Very Confused on Page 33
    ... On page 33 Bruce is explaining inheritance. ... > base class you can also send to objects of the derived class. ... The programmatic difference enables the subclass ...
    (comp.lang.java.help)
  • Re: attribute or subclass
    ... With inheritance the derived class code ... reference in place of the base class reference. ...
    (comp.object)
  • Re: C# inheritance broken?
    ... class as a derived class, you can do the opposite though. ... Ignacio Machin ... issue of inheriting a base class ... You can't use a base class instance as though it were a derived class ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Page_load not called in base class
    ... > Which derived class is hiding the base class ... Note that the base class already has a method called ... as Overrideable, Overrides, or MustOverride is available to (and, in fact, ... I'm not sure why implicitly hiding or shadowing a member in a base class is ...
    (microsoft.public.dotnet.framework.aspnet)