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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:48:12+00:00 2026-05-24T20:48:12+00:00

NB: I’m using the Mono environment, if that makes a difference. I’m writing some

  • 0

NB: I’m using the Mono environment, if that makes a difference.

I’m writing some tests, and to help with that I have the following generic method:

static MemoryStream writeValue<T>(T inVal)
{
  MemoryStream ms = new MemoryStream();
  BinaryWriter bw = new BinaryWriter(ms);
  encode_any(bw, inVal);
  return ms;
}

The idea is that this will call one of my encode_any overloads, which delegate to type-specific methods:

public static void encode_any(BinaryWriter writer, Int32 val) { encode_int32(writer, val); }
public static void encode_any(BinaryWriter writer, Int64 val) { encode_int64(writer, val); }
public static void encode_any(BinaryWriter writer, Float val) { encode_float(writer, val); }
...etc...
public static void encode_any(BinaryWriter writer, bool val) { encode_bool(writer, val); }

The reason I want to do this is so that I can call the right implementation in each case without needing several versions of ‘writeValue’, and without inspecting the run-time type of each item before choosing which encode_ function to call.

However it appears that the compiler is attempting to resolve the overload of writeValue without knowing what type T will be, so I get this error:

error CS1502: The best overloaded method match for ‘BinarySerialiser.encode_any(System.IO.BinaryWriter, bool)’ has some invalid arguments
error CS1503: Argument ‘#2’ cannot convert ‘T’ expression to type ‘bool’

I notice that this is not specifically about bool – if I rearrange the order of my encode_any functions, it always attempts to use the last one. Maybe it just tries each one and then only emits the error when it runs out of alternatives.

This sort of code would work fine under C++ where writeValue is only compiled when T is known, but that doesn’t appear to be the case here, and I suspect this is because C# handles generic types at runtime.

Can I change my approach to make this work, and if so how?

  • 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-24T20:48:13+00:00Added an answer on May 24, 2026 at 8:48 pm

    This is not C++ but C#. That means, the code you have isn’t working, because in a generic class in C# you have access to all known members of the generic type parameters. Which members are known is solely determined by the constraints you put on the generic type parameters. IIRC, in C++ it is a bit different.
    Anyway, in your case, there are no constraints on T, so the compiler knows nothing about that generic parameter. However, there is no constraint that would allow you to do, what you want to do.
    If you were using .NET 4.0, you could use the new dynamic keyword:

    static MemoryStream writeValue<T>(T inVal)
    {
      MemoryStream ms = new MemoryStream();
      BinaryWriter bw = new BinaryWriter(ms);
      dynamic dynamicValue = inVal;
      encode_any(bw, dynamicValue);
      return ms;
    }
    

    Because you are using Mono, I don’t think you can use this as Mono hasn’t implemented this keyword yet.
    It looks like Mono already implemented the DLR, see link from paolo below.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.