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 9125913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:54:53+00:00 2026-06-17T06:54:53+00:00

I am using C#/.NET 4.0 and a Protocol Buffers library (protobuf-net) which provides the

  • 0

I am using C#/.NET 4.0 and a Protocol Buffers library (protobuf-net) which provides the following functionality.

public static class Serializer {
    public static void Serialize<T>(Stream destination, T instance);
    public static void Serialize<T>(SerializationInfo info, T instance);
    public static void Serialize<T>(XmlWriter writer, T instance);
    public static void Serialize<T>(SerializationInfo info, StreamingContext context, T instance);
    public static T Deserialize<T>(Stream source);
}

I need to wrap two of these calls with non-generic equivalents. Specifically, I want

void SerializeReflection(Stream destination, object instance);
object DeserializeReflection(Stream source, Type type);

that simply call the respective generic members of Serializer at runtime. I have gotten the DeserializeReflection method to work with the following code:

public static object DeserializeReflection(Stream stream, Type type)
{
    return typeof(Serializer)
        .GetMethod("Deserialize")
        .MakeGenericMethod(type)
        .Invoke(null, new object[] { stream });
}

The SerializeReflection method is what is causing me trouble. I at first tried the following code:

public static void SerializeReflection(Stream stream, object instance)
{
    typeof(Serializer)
        .GetMethod("Serialize")
        .MakeGenericMethod(instance.GetType())
        .Invoke(null, new object[] { stream, instance });
}

The problem is that the part between typeof(Serializer) and .Invoke(...) is not working. The call to GetMethod("Serialize") gets me an AmbiguousMatchException, because there are four methods named “Serialize.”

I then tried using the overload of GetMethod that takes an array of System.Type to resolve the binding:

GetMethod("Serialize", new[] { typeof(Stream), instance.GetType() })

But this just made the result of GetMethod null.

How can I use reflection to get the MethodInfo for void Serializer.Serialize<T>(Stream, T), where T is instance.GetType()?

  • 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-06-17T06:54:55+00:00Added an answer on June 17, 2026 at 6:54 am

    Try to use next code snippet to see if it meets your need. It creates a close typed instance of method public static void Serialize<T>(Stream destination, T instance). In this case it select the first method with Stream as parameter, but you can change this predicate method.GetParameters().Any(par => par.ParameterType == typeof(Stream)) to whatever you want

    public static object DeserializeReflection(Stream stream, object instance)
    {
       return typeof(Serializer)
            .GetMethods()
            .First(method => method.Name == "Serialize" && method.GetParameters().Any(par => par.ParameterType == typeof(Stream)))
            .MakeGenericMethod(instance.GetType())
            .Invoke(null, new object[] { stream, instance });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a system that has a set of protocol buffers (using protobuf-net), I
i'm new to protocol buffers and I am using protobuf-net for VS2010. from what
I am using protobuf-net in my .net project to use protocol buffer. The project
I am using protocol buffer in .net http://code.google.com/p/protobuf-net/ . I installed the visual studio
I have been looking at protobuf-net recently and protocol buffers and so far it
Please, observe the following simple program (based on the example from the protobuf-net project
We are using a application protocol which specifies the length indicator of the message
I am using Net::SMPP package to handle SMPP protocol. One of the protocol parameters
I'm using ASP.NET, and IIS web server. I want to use HTTPS protocol on
Building a Monodroid application we've been using Protobuf-net for serialization. It works in debug

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.