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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:56:00+00:00 2026-05-17T02:56:00+00:00

Having the following generic class that would contain either string, int, float, long as

  • 0

Having the following generic class that would contain either string, int, float, long as the type:

public class MyData<T>
{
    private T _data;

    public MyData (T value)
    {
        _data = value;
    }

    public T Data { get { return _data; } }
}

I am trying to get a list of MyData<T> where each item would be of different T.

I want to be able to access an item from the list and get its value as in the following code:

MyData<> myData = _myList[0];    // Could be <string>, <int>, ...
SomeMethod (myData.Data);

where SomeMethod() is declared as follows:

public void SomeMethod (string value);
public void SomeMethod (int value);
public void SomeMethod (float value);

UPDATE:

SomeMethod() is from another tier class I do not have control of and SomeMethod(object) does not exist.


However, I can’t seem to find a way to make the compiler happy.

Any suggestions?

Thank you.

  • 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-17T02:56:00+00:00Added an answer on May 17, 2026 at 2:56 am

    Delegates can really help simplify this, and still keep things type-safe:

    public void TestMethod1()
    {
        Action<SomeClass, int> intInvoke = (o, data) => o.SomeMethod(data);
        Action<SomeClass, string> stringInvoke = (o, data) => o.SomeMethod(data);
    
        var list = new List<MyData> 
        {
            new MyData<int> { Data = 10, OnTypedInvoke = intInvoke },
            new MyData<string> { Data = "abc", OnTypedInvoke = stringInvoke }
        };
    
        var someClass = new SomeClass();
        foreach (var item in list)
        {
            item.OnInvoke(someClass);
        }
    }
    
    public abstract class MyData
    {
        public Action<SomeClass> OnInvoke;
    }
    
    public class MyData<T> : MyData
    {
        public T Data { get; set; }
        public Action<SomeClass, T> OnTypedInvoke 
        { set { OnInvoke = (o) => { value(o, Data); }; } }
    }
    
    public class SomeClass
    {
        public void SomeMethod(string data)
        {
            Console.WriteLine("string: {0}", data);
        }
    
        public void SomeMethod(int data)
        {
            Console.WriteLine("int: {0}", data);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains the following two properties: public int Id {
The following code does not compile: public class GenericsTest { public static void main(String[]
I'm having a class like the following: using System; using System.Collections.Generic; using System.Runtime.Serialization; [DataContract()]
I have the following classes: public class BagA : Dictionary<string, BagB> {} public class
I want a single generic class that can accept either reference or value types,
I am having following class hierarchy - //Abstract Class1 in library1 (Can't modify this)
I'm having following problem. I should convert a control to a certain type, this
i m having following type of simple sql server table Here I want to
I have a class similar to the following: public abstract class Manager<T, TInterface> :
I wrote the following method. public T GetByID(int id) { var dbcontext = DB;

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.