基本的にはタイトルの通り:
[DataContract(Name = "{0}Item")] //This will format properly
public class GenericItem<T>
{
[DataMember(Name = "The{0}")] //This will NOT format properly
public T TheItem { get; set; }
}
[CollectionDataContract(Name = "{0}Items")] //This will format properly
public SpecialCollection<T> : Collection<T> { }
[ServiceContract(Name = "{0}Service")] //This will NOT format properly
public interface IGenericService<T>
{
[OperationContract(Name = "Get{0}")] //This will NOT format properly
GenericItem<T> Get<T>();
}
さて、これで...何が機能し、何が機能しないのか...しかし、問題は...なぜですか? 明らかに、.NET は具体的な型を作成し、型を表すDataContract
andを使用するときに名前をフォーマットすることができますCollectionDataContract
(つまりGenericItem<Foo>
、 or 。では、も同様にフォーマットできるようにしSpecialCollection<Foo>
ないのはなぜですか?DataMember
上記ServiceContract/OperationContract
の方法で(ちょっと)理解できますが、理解できないのは、具体的なタイプを指定すると、操作がまだ正しく機能しないことです。
[ServiceContract(Name = "FooService")]
public interface FooService : IGenericService<Foo> { }
public interface IGenericService<T>
{
[OperationContract(Name = "Get{0}")] //This will NOT format properly
T Get<T>();
}
繰り返しますが、なぜですか?明らかに、ここで Foo の具象型を宣言しています。これは、IGenericService が IGenericService<Foo> であることを意味します。型を認識しているため、OperationContract 名をフォーマットするべきではありませんか?
アップデート:
一般的にフォーマットされたServiceContractを使用できないことに腹を立てた理由を思い出しました...サービスを実装するときに、具体的な型を指定しています...
//See! I gave it a concrete type to go by!
[ServiceBehavior(...)]
public class MyService : IGenericService<Foo> { ... }
このためのMicrosoft Connect要求を作成しました。他の属性にこの機能が必要な場合は、賛成票を投じてください。http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2327048-enable-generics-for-datamemberattribute-serviceco