Generic attirbute

rated by 0 users
This post has 1 Reply | 2 Followers

Top 25 Contributor
Points 280
Todor Posted: 05.10.2008 9:29

Еве едно интересно сценарио... ова подолу фрла грешка ...

 

Чаре?

 

[System.ServiceModel.ServiceKnownType(typeof(T))]
public class TestClass<T>
{
}

 

 

Good software is software that is designed to easily survive change.

  • | Post Points: 20
Top 10 Contributor
Points 2,900

ServiceKnownType атрибутот е наменет да се користи во WCF, поточно во креирање на сервисните контракти - интерфејси кои го дефинираат сервисот и неговите методи. Самиот познат тип (KnownType) кој се наведува е всушност тип кој мора да биде вклучен во процесот на серијализација и десеријализација.

Со други зборови, пробај да го дефинираш интерфејсот по кој мора да ти е специфициран генеричкиот тип во случајот и ќе ја постигнеш бараната цел.

Еве еден пример (copy-paste) од документацијата на MSDN (http://msdn.microsoft.com/en-gb/library/system.servicemodel.serviceknowntypeattribute.aspx):

// Define a service contract and apply the ServiceKnownTypeAttribute
// to specify types to include when generating client code.
// The types must have the DataContractAttribute and DataMemberAttribute
// applied to be serialized and deserialized. The attribute specifies the
// name of a method (GetKnownTypes) in a class (Helper) defined below.
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
[ServiceContract()]
public interface ICatalog
{
    // Any object type can be inserted into a Hashtable. The
    // ServiceKnownTypeAttribute allows you to include those types
    // with the client code.
    [OperationContract]
    Hashtable GetItems();
}

// This class has the method named GetKnownTypes that returns a generic IEnumerable.
static class Helper
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
        System.Collections.Generic.List<System.Type> knownTypes =
            new System.Collections.Generic.List<System.Type>();
        // Add any types to include here.
        knownTypes.Add(typeof(Widget));
        knownTypes.Add(typeof(Machine));
        return knownTypes;
    }
}

[DataContract()]
public class Widget
{
    [DataMember]
    public string Id;
    [DataMember]
    public string Catalog;
}

[DataContract()]
public class Machine : Widget
{
    [DataMember]
    public string Maker;
}


 

There is no substitute for good architecture and design, as well as honed development skills.

Page 1 of 1 (2 items) | RSS
Macedonian .NET User Group
Powered by Community Server (Commercial Edition), by Telligent Systems