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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:05:35+00:00 2026-06-07T05:05:35+00:00

I have a big dictionary of (string, object). The values in the dictionary are

  • 0

I have a big dictionary of (string, object). The values in the dictionary are of different types. Only at run time I can find the exact type of the values in the dictionary of (string, int) or (string, string). At run time I have to assign the values in the dictionary to their corresponding strongly typed objects. This is the simplified problem.
I’m trying to use a typed class that does the cast.
I have this code that does not work:

static void Main(string[] args)
{
    var values = new Dictionary<string, object> 
    { 
        { "123", "test"},
        {"12", 123}
    };
    var result = new Dictionary<string, object> ();

    Type dict = values.GetType();
    Type typedCast = typeof(TypedClass<>).MakeGenericType(new [] { dict });
    MethodInfo method = typedCast.GetMethod("GetTypedValue", 
        BindingFlags.Static | BindingFlags.Public, 
        null, 
        new[]
            {
                  typeof(object), 
                  typeof(object).MakeByRefType()
            },
        null);

    method.Invoke(null, new[]{values, result});
}

public class TypedClass<T>
{
    public static void GetTypedValue(object value, out object obj)
    {
        obj = (T)Convert.ChangeType(value, typeof(T));
    }
}

Inside the GetTypedValue method I see the obj value with the correct type, but outside this method the out variable has no values. Please let me know what am I doing wrong.

  • 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-07T05:05:36+00:00Added an answer on June 7, 2026 at 5:05 am

    Try replacing:

    method.Invoke(null, new[]{values, result});
    

    with

    var invokeArgs = new[]{values, result};
    method.Invoke(null, invokeArgs);
    //here you can check the invokeArgs[1] for the actual result of the conversion
    

    In any case, I’m not sure why you would want to convert the type of a Dictionary to another Dictionary, because that’s what your code seems to do…

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

Sidebar

Related Questions

Let's say we have a big read only list of (int, string) elements. What
I have big records to copy values from table B to table A. Currently
I have big dataset (but the following is small one for example). I can
I have a tough problem right now. I am having a big dictionary file
I have an object that consists primarily of a very large nested dictionary: class
So I have this huge tree that is basically a big switch/case with string
I have a Dictionary that I am using to avoid writing big if statements.
Is there a way to implement a compile-time type dictionary via C++ templates? e.g.
in my Class I have a static dictionary of strings object which contains a
Suppose that I have a Dictionary<string, string> . The dictionary is declared as public

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.