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

  • Home
  • SEARCH
  • 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 6547177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:47:54+00:00 2026-05-25T11:47:54+00:00

I use ProtoBuf to serialize class which is created at runtime using Activator.CreateInstance. Unfortunately

  • 0

I use ProtoBuf to serialize class which is created at runtime using Activator.CreateInstance.
Unfortunately Serializer.Deserialize method gives error “Type is not expected, and no contract can be inferred: System.Object”. Any hint how to solve this.

var converterName = "Passing class name as string"
var type = Type.GetType(converterName);
            var yourObject = Activator.CreateInstance(type) 
            if (yourObject != null)
            {

                FillRequest(Request.Params, yourObject);
                var com = new CommunicationLayer();
                yourObject = com.Submit(yourObject);
                FillResponse(yourObject);
            }

public class CommunicationLayer
{
public T Submit<T>(T engine)
{
<code skip>
 Serializer.Serialize(stream, engine); //Works fine
<code skip>

  engine = Serializer.Deserialize<T>(stream); //Gives error

<code skip>
}
}

I have choose to create and cast class at runtime from string because converterName variable parameter is passed in ASP.NET app and such classes will be about 100. Yes, I could replace the whole code with 100 If’s

If converterName=="MyClass1"
{
 var yourObject = new MyClass1();
 FillRequest(Request.Params, yourObject);
                var com = new CommunicationLayer();
                yourObject = com.Submit(yourObject);
                FillResponse(yourObject);
}
  Else  
If converterName=="MyClass2"
{
 var yourObject = new MyClass2();
 FillRequest(Request.Params, yourObject);
                var com = new CommunicationLayer();
                yourObject = com.Submit(yourObject);
                FillResponse(yourObject);
}
  Else  
....

but I would like to have less code if possible.

  • 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-25T11:47:55+00:00Added an answer on May 25, 2026 at 11:47 am

    That is because the generic method is inferring T=object.

    There is a non-generic API intended for this scenario; look at Serializer.NonGeneric.*

    Or in v2, everything under TypeModel/RuntimeTypeModel (where all the “real” code now is) is also non-generic throughout.

    If you are doing this lots, I recommend using v2. In v1 the generic code is the “primary”, with the non-generic code using reflection to shim into the generic code via MakeGenericMethod() (relatively expensive). In v2 this is reversed: the non-generic code is the “primary”, and the generic methods shim into the non-generic API via typeof(T).

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

Sidebar

Related Questions

Is it possible to use protobuf-net in RESTful webservices created using WCF RESTful starter
Using Protobuf-Net, I see that it does not seem possible to deserialize a class
I'd like to use protobuf-net to serialize a derived class as its base class.
I'm trying to use protobuf in a C# project, using protobuf-net, and am wondering
C#, How can I use ProtoBuf .NET to serialize a dataTable? Can I please
I'm using protobuf-net , and I'm trying to: Generate a C# class from a
I'm trying to serialize some objects with protobuf-net , but unfortunately they make liberal
In my solution, I have created public class to store value and already declare
We use .net binary serialization right now to serialize data to persist. It does
I was using a version of v2 of protobuf-net from a few weeks ago

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.