简体   繁体   English

有什么方法可以更改类的属性吗?

[英]Is there any way to change a class' attributes?

I am using Microsoft REST Stark Kit Preview 2 to explore REST Collection WCF Service. 我正在使用Microsoft REST Stark Kit Preview 2探索REST Collection WCF服务。 Within the Service.svc.cs class, there are some classes and interfaces being used as base or component classes. 在Service.svc.cs类中,有一些类和接口用作基类或组件类。 For example: 例如:

 public interface ICollectionService<TItem> where TItem : class
 {
    ...
    [WebHelp(Comment = "Returns the items in the collection in JSON format, along with URI links to each item.")]
    [WebGet(UriTemplate = "?format=json", ResponseFormat = WebMessageFormat.Json)]
    ItemInfoList<TItem> GetItemsInJoson();
    ...
  }

  [CollectionDataContract(Name = "ItemInfoList", Namespace = "")]
  public class ItemInfoList<TItem> : List<ItemInfo<TItem>> where TItem : class
  ...

where ICollectionServices and ItemInfoList are all in Microsoft.ServiceModel.Web.dll in the Preview 2. I would change those item's attributes such as WebHelp's Comment and CollectionDataContract's Name so that I could customize help message and templates for xml node names. 其中ICollectionServices和ItemInfoList都在Preview 2的Microsoft.ServiceModel.Web.dll中。我将更改那些项的属性,例如WebHelp的Comment和CollectionDataContract的Name,以便为XML节点名称自定义帮助消息和模板。 The Preview 2's change with embedding those interfaces and classes in its dll makes it difficult to do any customization. 预览2的更改是将这些接口和类嵌入其dll中,因此很难进行任何自定义。

So my question is that if there is any way to change a class or interface's attributes or overwrite their existing attributes so that I don't need to get the source codes to make changes? 所以我的问题是,是否有任何方法可以更改类或接口的属性或覆盖其现有属性,从而无需获取源代码进行更改?

No, you can't. 不,你不能。

What you might be able to do is inherit from the classes. 您可能能够做的就是从这些类继承。 If the attributes in question are not inheritable, you can add your own to your subclasses to override them. 如果所讨论的属性不可继承,则可以将自己的属性添加到子类中以覆盖它们。

I checked the CollectionDataContractAttribute , and it, at least, is not inheritable. 我检查了CollectionDataContractAttribute ,至少它是不可继承的。 That means if you create a subclass, you can apply a different CollectionDataContract attribute to that subclass. 这意味着,如果您创建一个子类,则可以将另一个CollectionDataContract属性应用于该子类。

[CollectionDataContract(Name = "MyItemInfoList", Namespace = "MyNamespace")]
public class MyItemInfoList<TItem> : ItemInfoList<TItem> where TItem : class

However, with members, this approach will only work if they are virtual, so you can override them. 但是,对于成员,此方法仅在虚拟的情况下才有效,因此您可以覆盖它们。

Attributes are burnt in at compile time, and cannot for reflection be set at runtime. 属性在编译时被烧入,不能在运行时设置以进行反射 There are a few things you can do in "ComponentModel", but they wouldn't apply here. 您可以在“ ComponentModel”中做一些事情,但是在这里不适用。 The only other common case here is for "i18n", where an attribute might be designed to pick up different values at runtime (from resx etc, based on a key rather than the description being set in the code) - and again, this doesn't apply in this case. 此处唯一的其他常见情况是“ i18n”,该属性可能被设计为在运行时(从resx等中,基于键而不是在代码中设置的描述)拾取不同的值-再次,这没有在这种情况下不适用。

So, no. 所以不行。

In terms of REST Start kit Preview 2's customization issue, it look like the customization was disabled when the template basic classes are moved to its core dll. 在REST Start kit Preview 2的自定义问题方面,当模板基本类移至其核心dll时,似乎已禁用了自定义。 According to WCF REST Start kit forum at ASP.NET, the customization features will be back in the next release (soon). 根据ASP.NET上的WCF REST Start kit论坛,自定义功能将在下一个版本(很快)中恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM