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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:23:30+00:00 2026-06-07T04:23:30+00:00

Id like to be able to work with Dictionaries in generic methods. What Im

  • 0

Id like to be able to work with Dictionaries in generic methods. What Im looking for is a way to get the Type from Key and Value of the dictionary sent to the generic extension method LoadProperty.

This is what I’ve done so far.

I call the method as an extension

entityObject.LoadProperty<Dictionary<string, int>>("CartonThreshold")
//entityObject.LoadProperty<Dictionary<string, double>>("CartonThreshold")

// ... 

public static T LoadProperty<T>(this EntityObject entity, string name) where T : new()
{
    EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));

    // If request dictionary
    if (typeof(T).GetInterface(typeof(IDictionary<,>).Name) != null || typeof(T).Name.Contains("IDictionary"))
    {
        var dictionaryInstance = (IDictionary)new T();

        // Just for testing
        var a = dictionaryInstance.Keys.GetType().Name;
        var b = dictionaryInstance.Values.GetType().Name;

        var key = (T) Convert.ChangeType(prop.Name, typeof (T));
        var value = (T) Convert.ChangeType(prop.Value, typeof (T));
        dictionaryInstance.Add(key, value);
        return (T) dictionaryInstance;
    }

    // default
    return (T)Convert.ChangeType(prop.Value, typeof(T));
}

The goal is to return a correctly typed dictionary

  • 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-07T04:23:33+00:00Added an answer on June 7, 2026 at 4:23 am

    What I was looking for was the GetGenericArguments() extension of GetType()

    The method below will return what I need.

    public static T LoadProperty<T>(this EntityObject entity, string name) where T : new()
    {
        EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));
    
        try
        {
            // If request dictionary
            if (typeof(T).GetInterface(typeof(IDictionary<,>).Name) != null || typeof(T).Name.Contains("IDictionary"))
            {
                var dictionaryInstance = (IDictionary)new T();
    
                var typing = dictionaryInstance.GetType().GetGenericArguments();
                Type keyType = typing[0];
                Type valueType = typing[1];
    
                // dictionary fallback, set to default of the valuetype if null
                object value = prop.Value != null ? Convert.ChangeType(prop.Value, valueType) : Activator.CreateInstance(valueType);
                var key = Convert.ChangeType(prop.Name, keyType);
                dictionaryInstance.Add(key, value);
                return (T)dictionaryInstance;
            }
    
            if (prop.Value != null)
            {
                // default
                return (T)Convert.ChangeType(prop.Value, typeof(T));
            }
        }
        catch { }
        return default(T);
    }
    

    This way I can call the method like this

    // Will return a typed dictionary with the EntityObjectProperty name as key and the EntityObjectProperty Value as value
    entityObject.LoadProperty<Dictionary<string, int>>("CartonThreshold")
    
    // Will return a string of the EntityObjectProperty Value
    entityObject.LoadProperty<string>("CartonThreshold")
    
    // Will return an Int of the EntityObjectProperty Value
    entityObject.LoadProperty<int>("CartonThreshold")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I'm coming from C#-land, so I'm looking for something like being able to
I would like to be able to import TFS tasks / work items into
I'd like to be able to programmatically save an image from a page to
I'm finally set up to be able to work from home via VPN (using
I'm writing some code that id like to be able to work with any
This sounds like a simple question, but I haven't been able to work it
I want some of the goodies in a ListView, like being able to use
Would like to be able to set colors of headings and such, different font
Id like to be able to create checkboxes for a list of objects. Then
I'd like to be able to fix the position of certain rows in a

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.