自定义属性,共享方法,派生类和反射 [英] Custom Attributes, Shared methods, Derived classes and reflection

查看:52
本文介绍了自定义属性,共享方法,派生类和反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在这里有一个反思大脑迷雾,也许有人可以把我放在右边

轨道。


我想定义一个在类层次结构中使用的自定义属性。


我想要的是拥有一个属性可以应用于类定义

继承自base,mustinherit类的类。我想在基类中定义方法

,它将访问属性的内容,因为它应用于

a特定的派生类。


这在处理派生类的实例时相当容易。

但是,我需要能够在基类中共享这些方法,所以

属性可以按派生类的定义访问,而不是在类的实例

上访问。


也就是说,对于实例方法我用我的自定义属性

定义派生类,比如说:


< MyCustomAttribute(" String1"," String2") _

Public MyDerivedClass

继承MyBaseClass

...


然后,在基类中,我定义了一个类似的方法:


Public ReadOnly属性ValueFromMyAttribute()as string

Get

Dim classtype As Type = Me .GetType

Dim attr As EntityAttrib ute =

DirectCast(Attribute.GetCustomAttribute(classtype,_

GetType(MyCustomAttribute)),MyCustomAttribute)

如果attr IsNot Nothing那么

返回attr.PropertyForString1

否则

返回classtype.Name

结束如果

结束获取

结束财产


这似乎有用。


但是,现在需要访问属性来自SHARED

方法的属性,因为即使没有派生类的实例

,也可能需要这些值。


看起来这应该是可能的,但是怎么样?如果我共享该属性,

那么显然没有我的实例。引用,所以这会产生编译

错误。我怎样才能获得派生CLASS的类型而不是来自

那个类的实例?我假设如果我可以得到它,我可以跟踪属性并访问

其属性如上所述。


谢谢,


- 杰夫

Hi,

I''m having a reflection brain fog here, perhaps someone can set me on the right
track.

I''d like to define a custom attribute to be used in a class hierarchy.

What I want to do is to have an attribute which can be applied to a class definition
of a class which inherits from a base, mustinherit class. I want to define methods
in the base class which will access the contents of the attribute as it is applied to
a particular derived class.

This was reasonably easy to do when dealing with an instance of the derived class.
But, I need to be able to have these methods in the base class be shared, so the
attribute can be accessed as defined on the derived class, rather than on an instance
of the class.

That is, for the instance method I define a derived class with my custom attribute
as, say:

<MyCustomAttribute("String1", "String2")_
Public MyDerivedClass
Inherits MyBaseClass
...

Then, in the base class, I defined a method like:

Public ReadOnly Property ValueFromMyAttribute() as string
Get
Dim classtype As Type = Me.GetType
Dim attr As EntityAttribute =
DirectCast(Attribute.GetCustomAttribute(classtype, _
GetType(MyCustomAttribute)), MyCustomAttribute)
If attr IsNot Nothing Then
Return attr.PropertyForString1
Else
Return classtype.Name
End If
End Get
End Property

This seemed to work.

But, the requirement now exists to access the attribute''s properties from SHARED
methods, since the values may be needed even if no instance of the derived class
exists.

It seems like this ought to be possible, but how? If I make the property shared,
then obviously there is no instance for "Me" to refer to, so this generates a compile
error. How can I get the type of the derived CLASS instead of from an instance of
that class? I assume if I can get that, I can track down the attribute and access
its properties as I did above.

Thanks,

-- Jeff

推荐答案

你好杰夫,


如果问题是只是为了获得你可以使用的类型

GetType(YourBaseClass)...

但我有点困惑,因为它似乎没有意义

属性

从一个实例中分离出来。


你能举一个有意义的例子。为什么你需要



使用属性来做到这一点?


-tom


Jeff Mason ha scritto:
Hi Jeff,

if the problem were just to obtain the type you could use
GetType(YourBaseClass) ...
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 ?

-tom

Jeff Mason ha scritto:




我这里有反射脑雾,也许有人可以把我放在右边

轨道。


我想定义一个在类层次结构中使用的自定义属性。


我想要做的是拥有一个可以应用于类定义的属性

继承自base,mustinherit类的类。我想在基类中定义方法

,它将访问属性的内容,因为它应用于

a特定的派生类。


这在处理派生类的实例时相当容易。

但是,我需要能够在基类中共享这些方法,所以

属性可以按派生类的定义访问,而不是在类的实例

上访问。


也就是说,对于实例方法我用我的自定义属性

定义派生类,比如说:


< MyCustomAttribute(" String1"," String2") _

Public MyDerivedClass

继承MyBaseClass

...


然后,在基类中,我定义了一个类似的方法:


Public ReadOnly属性ValueFromMyAttribute()as string

Get

Dim classtype As Type = Me .GetType

Dim attr As EntityAttribute =

DirectCast(Attribute.GetCustomAttribute(classtype,_

GetType(MyCustomAttribute)),MyCustomAttribute)

如果attr IsNot Nothing然后

返回attr.PropertyForString1

否则

返回classtype.Name

结束如果

结束获取

结束物业


这似乎有用。


但是,现在的要求是从SHARED

方法访问属性的属性,因为即使没有派生类

的实例,也可能需要这些值。

看起来这应该是可能的,但是怎么样?如果我共享该属性,

那么显然没有我的实例。引用,所以这会产生编译

错误。我怎样才能获得派生CLASS的类型而不是来自

那个类的实例?我假设如果我可以得到它,我可以跟踪属性并访问

其属性如上所述。


谢谢,


- Jeff
Hi,

I''m having a reflection brain fog here, perhaps someone can set me on the right
track.

I''d like to define a custom attribute to be used in a class hierarchy.

What I want to do is to have an attribute which can be applied to a class definition
of a class which inherits from a base, mustinherit class. I want to define methods
in the base class which will access the contents of the attribute as it is applied to
a particular derived class.

This was reasonably easy to do when dealing with an instance of the derived class.
But, I need to be able to have these methods in the base class be shared, so the
attribute can be accessed as defined on the derived class, rather than on an instance
of the class.

That is, for the instance method I define a derived class with my custom attribute
as, say:

<MyCustomAttribute("String1", "String2")_
Public MyDerivedClass
Inherits MyBaseClass
...

Then, in the base class, I defined a method like:

Public ReadOnly Property ValueFromMyAttribute() as string
Get
Dim classtype As Type = Me.GetType
Dim attr As EntityAttribute =
DirectCast(Attribute.GetCustomAttribute(classtype, _
GetType(MyCustomAttribute)), MyCustomAttribute)
If attr IsNot Nothing Then
Return attr.PropertyForString1
Else
Return classtype.Name
End If
End Get
End Property

This seemed to work.

But, the requirement now exists to access the attribute''s properties from SHARED
methods, since the values may be needed even if no instance of the derived class
exists.

It seems like this ought to be possible, but how? If I make the property shared,
then obviously there is no instance for "Me" to refer to, so this generates a compile
error. How can I get the type of the derived CLASS instead of from an instance of
that class? I assume if I can get that, I can track down the attribute and access
its properties as I did above.

Thanks,

-- Jeff


2006年7月31日01:54:21 -0700,到************** @ uniroma1.it 写道:
On 31 Jul 2006 01:54:21 -0700, to**************@uniroma1.it wrote:

>你好Jeff,

如果问题只是为了获得你可以使用的类型
GetType(YourBaseClass)...
>Hi Jeff,

if the problem were just to obtain the type you could use
GetType(YourBaseClass) ...



我想我尝试了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 ?



我希望避免为什么你需要这样做各种各样的问题,因为他们可以分散手头的问题,但是好的。


我想要定义的属性是 ;友好名称"属性。这个名称将在

中使用,例如,涉及该类的错误消息。我可以定义一个叫做EntLOB的类叫做
,但是我想使用一个更友好的名字,比如企业行

of Business。当提出涉及该课程的错误时。


多次出现友好名称与类名相同(比如Customer),但是在多个案例中我可以用作程序员的类名不是最好的

选择作为向最终用户呈现的名称。


几乎所有错误消息都涉及到类的实例,但

考虑根本无法创建对象实例的情况。也许

用户输入了一些数据(例如搜索条件),这会阻止创建实例
。网络可能会崩溃。数据库可能处于脱机状态。我想要

错误信息如下:


无法检索企业业务线,因为......


而不是


无法检索EntLOB因为...


前者更多,呃,友好由于数据库脱机,因此无法构建对象,并且没有可用于获取类型。由于我可能创建的所有对象都来自一个公共基类,我想根据需要用这个属性来装饰我的派生类

,并且有一个常用方法,在基础

类中实现,它访问该属性并从

属性返回指定的名称。


因为即使没有派生的

类的特定实例,我可能需要访问此属性,我想在基类中定义一个共享方法,它返回

友好派生类的名称(即而不是实例的名称),

是属性所在的位置。我希望我可以使用如下语法:


errormsg = string.format("无法检索{0} ...,EntLOB.FriendlyName,...) ...


其中FriendlyName是在基类中实现的共享只读属性。


HTH,


- 杰夫

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


嗨杰夫,


你不能,如果有错误,只需创建虚拟

衍生类的实例,

如你所愿装饰?


在这种情况下你也可以改变属性取决于

异常类型。

您认为这有意义吗?


-tom


Jeff Mason ha scritto:
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:

2006年7月31日01:54:21 -0700,到************** @ uniroma1.it 写道:
On 31 Jul 2006 01:54:21 -0700, to**************@uniroma1.it wrote:

你好Jeff,


如果问题只是为了获得你可以使用的类型

GetType(YourBaseClass)...
Hi Jeff,

if the problem were just to obtain the type you could use
GetType(YourBaseClass) ...



我想我尝试了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 ?



我希望避免为什么你需要那样做各种各样的问题,因为他们可以分散手头的问题,但是好的。


我想要定义的属性是 ;友好名称"属性。这个名称将在

中使用,例如,涉及该类的错误消息。我可以定义一个叫做EntLOB的类叫做
,但是我想使用一个更友好的名字,比如企业行

of Business。当提出涉及该课程的错误时。


多次出现友好名称与类名相同(比如Customer),但是在多个案例中我可以用作程序员的类名不是最好的

选择作为向最终用户呈现的名称。


几乎所有错误消息都涉及到类的实例,但

考虑根本无法创建对象实例的情况。也许

用户输入了一些数据(例如搜索条件),这会阻止创建实例
。网络可能会崩溃。数据库可能处于脱机状态。我想要

错误信息如下:


无法检索企业业务线,因为......


而不是


无法检索EntLOB因为...


前者更多,呃,友好由于数据库脱机,因此无法构建对象,并且没有可用于获取类型。由于我可能创建的所有对象都来自一个公共基类,我想根据需要用这个属性来装饰我的派生类

,并且有一个常用方法,在基础

类中实现,它访问该属性并从

属性返回指定的名称。


因为即使没有派生的

类的特定实例,我可能需要访问此属性,我想在基类中定义一个共享方法,它返回

友好派生类的名称(即而不是实例的名称),

是属性所在的位置。我希望我可以使用如下语法:


errormsg = string.format("无法检索{0} ...,EntLOB.FriendlyName,...) ...


其中FriendlyName是在基类中实现的共享只读属性。


HTH,


- Jeff


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


这篇关于自定义属性,共享方法,派生类和反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆