Le réseau pour les développeurs > Forums - Accueil > Code Contracts > How do I put a contract on a generic interface?
Poser une questionPoser une question
 

TraitéeHow do I put a contract on a generic interface?

  • jeudi 26 février 2009 11:33OpinionatedGeek Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    Say I have the following interface:

    public interface IParser<T>
    {
        T Parse (
    string toParse);
    }

    How do I specify the attributes for the contract class?

    My initial thought was:

    [ContractClass (typeof (IParserContract<T>))]
    public interface IParser<T>
    {
        T Parse (
    string toParse);
    }

    and

    [ContractClassFor (typeof (IParser<T>))]
    public class IParserContract<T>
    {
        ...
    }

    But that won't work, because you can't use the generic T argument in the attributes.  And leaving the generic argument out won't work, because that doesn't specify the right interface/type.

    So, am I missing the proper way of doing this, or is this a limitation?

    Cheers,

        Geoff

Réponses

  • jeudi 26 février 2009 15:26Manuel FahndrichMSFT, PropriétaireMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    Excellent question and we probably should put this into the documentation. For generic interface contracts, the typeof has to refer to the non-instantiated generic type. This is a little known C# feature. It looks as follows:

    [ContractClass (typeof (IParserContract<>))]  
    public interface IParser<T>  
    {  
        T Parse (string toParse);  
    }  
     
    [ContractClassFor (typeof (IParser<>))]  
    public class IParserContract<T>  
    {  
        ...  
    }  
     
     

    Note the empty instantiation brackets IParserContract<> and IParser<>.

    -MaF

Toutes les réponses

  • jeudi 26 février 2009 15:26Manuel FahndrichMSFT, PropriétaireMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    Excellent question and we probably should put this into the documentation. For generic interface contracts, the typeof has to refer to the non-instantiated generic type. This is a little known C# feature. It looks as follows:

    [ContractClass (typeof (IParserContract<>))]  
    public interface IParser<T>  
    {  
        T Parse (string toParse);  
    }  
     
    [ContractClassFor (typeof (IParser<>))]  
    public class IParserContract<T>  
    {  
        ...  
    }  
     
     

    Note the empty instantiation brackets IParserContract<> and IParser<>.

    -MaF
  • vendredi 27 février 2009 00:48Keith Farmer Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    For multi-parameter generics, as you might imagine, you can use IFoo<,,,,> -- just drop the generic identifiers, leaving the commas, and you're good to go.

    This is also useful in instantiating generic types via reflection.  typeof(List<>) returns a valid type awaiting parameterization.


    Keith J. Farmer [Idea Entity]
  • vendredi 27 février 2009 10:03OpinionatedGeek Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Manuel Fahndrich said:

    For generic interface contracts, the typeof has to refer to the non-instantiated generic type. This is a little known C# feature.

    That's excellent - many thanks.  And I thought I knew most of the little-known C# features...!

    Cheers,

    Geoff