How to specify a WCF known type in config that is generic?

前端 未结 2 2015
情歌与酒
情歌与酒 2021-02-03 13:36

I have a type, let\'s call it Data. I also have a WCF service contract that accepts a type (lets call it Wrapper) with a property of type

2条回答
  •  悲哀的现实
    2021-02-03 13:47

    A generic type is instantiable from a string, if the string follows this pattern: Class name followed by a "`" character, followed by the number of type parameters(in this case it's 1), followed by the type parameters enclosed within "[]", and using comma as type parameter separator.

    
      
        
          
            
              
              
              
            
          
        
      
    
    

    Edit: I might also add, that if assembly information needs to be specified for the type parameters(althoug it's not the case for stuff in mscorlib), then nested "[]" is used.

    
    

    Edit: You can customize names of generic types in data contracts, using the string format pattern.

    [DataContract(Name = "Data{0}")]
    public class Data
    {...}
    

    By default, the name generated for the Data type is something like "DataOfInt32HJ67AK7Y", where "HJ67AK7Y" is a hash generated from the string "urn:default", or the namespace of your class, if you have any. But "Data{0}" would give it the name "DataInt32".

    More here. Have a look at the "Customizing Data Contract Names for Generic Types" part down the page.

提交回复
热议问题