Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 255123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:54:15+00:00 2026-05-11T21:54:15+00:00

I have a type, let’s call it Data<TKey> . I also have a WCF

  • 0

I have a type, let’s call it Data<TKey>. I also have a WCF service contract that accepts a type (lets call it Wrapper) with a property of type Object (for reasons I won’t go into, this isn’t optional).

[DataContract]
public class Data<TKey> { ... }

[DataContract]
public class Wrapper
{
    [DataMember]
    public object DataItem { get; set; }
}

Right now I’m sending two classes IntData and LongData:

[DataContract]
public class IntData : Data<int> { /*empty*/ }

[DataContract]
public class LongData : Data<long> { /*empty*/ }

They’re both configured in the known types config file. The config resembles something like this:

<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Wrapper, TheirAssembly">
          <knownType type="IntData, MyAssembly"/>
          <knownType type="LongData, MyAssembly"/>
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
</configuration>

At this point, everything works fine.

But I’m about to add a third type and I don’t like having the unnecessary, empty .NET classes IntData and LongData. They only exist because…

I don’t know how to specify generic types in WCF configuration!

I want to do something like this, but don’t know the exact syntax.

<configuration>
  <system.runtime.serialization>
    <dataContractSerializer>
      <declaredTypes>
        <add type="Wrapper, TheirAssembly">
          <!-- this syntax is wrong -->
          <knownType type="Data{System.Int32}, MyAssembly"/>
          <knownType type="Data{System.Int64}, MyAssembly"/>
        </add>
      </declaredTypes>
    </dataContractSerializer>
  </system.runtime.serialization>
</configuration>

What is the correct syntax for this?

(Note too that I cannot put [KnownType(...)] attributes on Wrapper as it’s not my type. Config seems to be the only way.)

EDIT

@baretta’s answer worked nicely. Note however that initially I received this error:

Type ‘MyAssembly.Data`1[System.Int64]’ cannot be added to list of known types since another type ‘MyAssembly.Data`1[System.Int32]’ with the same data contract name ‘http://www.mycompany.com/MyAssembly:Data‘ is already present.

I didn’t mention it in the original question, but my type has an explicit data contract name. Something like this:

[DataContract(Name = "Data")]
public class Data<TKey> { ... }

The above error occurred until I removed the Name property value from the attribute. Hope that helps someone else out too. I don’t know what format works in this scenario. These didn’t:

[DataContract(Name = "Data\`1")]
[DataContract(Name = "Data{TKey}")]

Anyone know how to do this?

EDIT 2

Thanks again to @baretta who pointed out that the correct syntax is in fact:

[DataContract(Name = "Data{0}")]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:54:15+00:00Added an answer on May 11, 2026 at 9:54 pm

    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.

    <configuration>
      <system.runtime.serialization>
        <dataContractSerializer>
          <declaredTypes>
            <add type="Wrapper, TheirAssembly">
              <!-- this syntax is all good -->
              <knownType type="Data`1[System.Int32], MyAssembly"/>
              <knownType type="Data`1[System.Int64], MyAssembly"/>
            </add>
          </declaredTypes>
        </dataContractSerializer>
      </system.runtime.serialization>
    </configuration>
    

    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.

    <knownType type="Data`1[[System.Int32, mscorlib]], MyAssembly"/>
    

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

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

    By default, the name generated for the Data<Int32> 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MySQL can't make a fulltext (or any) index accross multiple… May 12, 2026 at 1:14 am
  • Editorial Team
    Editorial Team added an answer I'm happy to tell you that you're right and that… May 12, 2026 at 1:14 am
  • Editorial Team
    Editorial Team added an answer I strongly suspect that wrapping the calls would be a… May 12, 2026 at 1:14 am

Related Questions

I have a type, let's call it Data<TKey> . I also have a WCF
I have a function that checks whether a type is a subtype of another
Let's say I have a socket connection, and the 3rd party listener on the
Let's say I have a class (MyClass.cs) in App_Code (using a ASP.NET web site,
I have a situation where an object A has a reference to object B.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.