3

I have a method which is something like below and i want to set the value of it with input string.

How would i go about it? Any advice will be greatly appreciated

private static void QueueCheckNAdd<T>(ref T param, string input)
    {
        param.DoSomethingLikeSetValue(input);
    }

for your reference, the generic type is something like int or double

3
  • 1
    If T will only be either int or double, you may want to just write a couple concrete methods to perform the conversion using int.Parse() and double.Parse(). Your method is not really generic if it can only be used with a small, fixed set of types. Using concrete methods also allows you to write the methods without casts.
    – antiduh
    Commented Mar 8, 2016 at 1:43
  • Side note: there is no really good way to restrict generic type to int/double - so if you are writing code that may be used you need to verify that it can work with all allowed types.... Commented Mar 8, 2016 at 1:46
  • I wanted to consider that one day i might have to accommodate for different types. I meant that right now its only assigned to data types of int and double
    – CJC
    Commented Mar 8, 2016 at 2:09

3 Answers 3

10
param = (T)(object)Convert.ChangeType(input, typeof(T));

The casts are necessary to convince the compiler that the result is really of type T.

2
  • 1
    I got it. Thank you very much
    – CJC
    Commented Mar 8, 2016 at 2:00
  • This has been driving me nuts for days! How did you figure this bit arcane magic out? Commented May 10, 2022 at 0:25
2

You want param to be generic (i.e., any type), and you expect to be able to call some method on it, correct? Well, you can see the problem there: if param can be any type, there's no way to guarantee that it will have the method DoSomethingLikeSetValue (or whatever). I'm sure you could get fancy with introspection or runtime type coercion, but I think the "clean" way to do what you're looking for is to constrain the type of T to some interface that has the required method (DoSomethingLikeSetValue). Like this:

private static void QueueCheckNAdd<T>(ref T param, string input) where T : IHasSomething {
    param.DoSomethingLikeSetValue(input);
}

public interface IHasSomething {
    void DoSomethingLikeSetValue(string s);
}

Then you can invoke QueueCheckNAdd generically only if the generic type supports the IHasSomething interface. So you could use it like this:

public class Foo : IHasSomething {
    public void DoSomethingLikeSetValue(string s) {
        Console.WriteLine(s);
    }
}

var f = new Foo();
QueueCheckNAdd<Foo>(f, "hello");
3
  • While good suggestion you can't really do that for built in types as you can't add interfaces to existing types... Commented Mar 8, 2016 at 1:47
  • That's true, Alexei...and that begs the question "if you can't do it, should you be doing it?" At the end of the day, if type safety is such a huge inconvenience, you should probably think about using a type-free language. Yes, this problem can be solved by RTTI or other methods, but it indicates to me that there's a bigger architectural issue. Commented Mar 8, 2016 at 2:00
  • This is also a very nice method, i will keep this in mind. But i will go with SLaks's answer because it is even cleaner. Although it may be slower in performance
    – CJC
    Commented Mar 8, 2016 at 2:01
1

Good practice would be to use interface like described before,

But if you want some fun, you could aslo use the object as a dynamic object, like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class SMTHG
    {
        public void DoSomethingLikeSetValue(string input)
        {
            Console.WriteLine("HEYYYYY!!! DYNAMIC OBJECTS FTW!...\n" + input);
        }
    }
    class Program
    {
        private static void QueueCheckNAdd<T>(ref T param, string input)
        {
            dynamic dynamicObject = (dynamic)param; 
            dynamicObject.DoSomethingLikeSetValue(input);
        }
        static void Main(string[] args)
        {
            SMTHG smthg = new SMTHG();
            QueueCheckNAdd(ref smthg, "yoyuyoyo");
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.