tags:

views:

183

answers:

3

I am using Microsoft REST Stark Kit Preview 2 to explore REST Collection WCF Service. Within the Service.svc.cs class, there are some classes and interfaces being used as base or component classes. 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. The Preview 2's change with embedding those interfaces and classes in its dll makes it difficult to do any customization.

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?

+1  A: 

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. 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.

So, no.

Marc Gravell
OK. I guess that the Preview 2 either provides alternative way or API for customization or I have use different project model to build my REST service with more codes.
David.Chu.ca
+2  A: 

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. That means if you create a subclass, you can apply a different CollectionDataContract attribute to that subclass.

[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.

Tor Haugen
I tried this strategy to create my class based on ItemInfoList. However, the ItemInfoList is a return type of CollectionServiceBase's members. CollectionServiceBase is in dll. I hit to the wall again.
David.Chu.ca
A: 

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. According to WCF REST Start kit forum at ASP.NET, the customization features will be back in the next release (soon).

David.Chu.ca