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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:53:51+00:00 2026-05-27T23:53:51+00:00

I have a generic dictionary of objects where the key is of type Type

  • 0

I have a generic dictionary of objects where the key is of type Type:

public class DynamicObject : IDictionary<Type, object>

The idea is that this object is shared in a plugin-based architecture and so a type (which could reside in a plugin .dll) is used as a key in order to prevent clashes between plugins. This type is also used to store metadata about the field (such as a description of that field or the actual type of that field).

Currently plugins need to set the value of fields on this object using code similar to this:

DynamicObject someObject;
string someValue;
someObject[typeof(UsernameField)] = someValue;

The only problem with this is that this isn’t type safe – even though the type UsernameField is aware of the exact type of the value it is expecting (e.g. int or in this case string), the value supplied here is just typed as an object. I’d like to use generics to make setting / getting of properties type safe but I’m not sure how. So far the best I’ve come up with is this:

// Field is a base class for all types used as keys on DynamicObject
[Description("Username of the user")]
public class UsernameField : Field
{
    public static void Set(DynamicObject obj, string value)
    {
        obj[typeof(UsernameField)] = someValue;
    }
}

// To set fields
UsernameField.Set(obj, someValue);

This is type safe, however it means that each of my field types (e.g. UsernameField) has a nearly identical static Set method.

How can I have type-safe access to values in this way without having lots of nearly identical methods on each of my field types?

As an aside, is using Type as a key like this a good idea or are there hidden pitfalls that I’m not yet aware of?

  • 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-27T23:53:52+00:00Added an answer on May 27, 2026 at 11:53 pm

    Define an interface that all your plugins must implement:

    public interface IPlugin {
        string Name { get; }
        string Author { get; }
        string Description { get; }
        void Init();
    }
    

    And then use a Dictionary<Type, IPlugIn>.

    Typically the interface is declared in a separate dll (like “MyCompany.MyProject.PlugIns.Contracts.dll”).


    EDIT: Ok, I think that I know what you mean now.

    The trick is to have a generic class between Field and UsernameField with a generic Set method. The fact that Field is not generic, makes all the field types assignable to it. This would not be the case, if it was declared as Field<T>.

    public abstract class Field
    {
    }
    
    public abstract class GenericField<T> : Field
    {
        public void Set(DynamicObject obj, T value)
        {
            obj[this.GetType()] = value;
        }
    }
    
    public class UsernameField : GenericField<string>
    {
        #region Singleton Pattern
    
        public static readonly UsernameField Instance = new UsernameField();
    
        private UsernameField() { }
    
        #endregion
    
    }
    

    Because we need to call GetType in the Set method, we cannot declare it as static. Therefore, I used the singleton pattern.

    Now, we can set the field in a type safe way:

    UsernameField.Instance.Set(obj, "Joe");
    

    ADDITION 1:

    Since now the fields are singletons, you could use the fields as key of the dictionary instead of their type.

    public class DynamicObject : IDictionary<Field, object> { }
    

    And Set would become:

    public void Set(DynamicObject obj, T value)
    {
        obj[this] = value;
    }
    

    ADDITION 2:

    You could also define DynamicObject like this:

    public class DynamicObject : Dictionary<Field, object>
    {
        public void Set<T>(GenericField<T> field, T value)
        {
            this[field] = value;
        }
    }
    

    Now you can set values like this:

    obj.Set(UsernameField.Instance, "Sue");
    

    This is type safe and seems more natural. The Set method in GenericField is obsolete now.

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

Sidebar

Related Questions

I have generic type that looks like: public class GenericClass<T, U> where T :
Say that i have a generic dictionary with data like this (I hope the
I have a class that inherits from a generic dictionary as follows: Class myClass
I have this method to get a generic repository out of a dictionary: public
I have a System.Collections.Generic.Dictionary<System.Web.UI.Control, object> where all keys can be either type of System.Web.UI.WebControls.HyperLink
I have two Generic lists having objects of class : class Subject { public
I have a Dictionary to map a certain type to a certain generic object
I have the following classes: public class DictionaryBuilder<T> where T : IDictionary<string, object>, new()
I have an object that contains a property: public Dictionary<string, List<Hotel>> CityHotels { get;
I have a collection of stuffs in a generic Dictionary<string, object>() . The key

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.