2

Is it possible at all to check if a type has a parameterless constructor, in order to cast it and call a method which requires a parameterless constructor with the : new() constraint?

Being only able to only check that a type as a public parameterless as answered here would not be enough as it would not allow calling the target methods.

The objective is to have the following logic, where IInteresting objects do not implement a public parameterless constructor and need to be converted before calling Save1:

    public interface IInteresting { }

    public void Save<T>(T o) {
        var oc = o as (new()); /* Pseudo implementation */
        if (oc != null) {
            this.Save1(oc);
        }
        else {
            var oi = o as IInteresting;
            if (oi != null) {
                this.Save2(oi);
            }
        }
    }

    private void Save1<T>(T o) where T : new() {
        //Stuff
    }

    private void Save2<T>(IInteresting o) {
        //Stuff to convert o to a DTO object with a public parameterless constructor, then call Save1(T o)
    }

Of course if I could make Save1 and Save2 share the same signature that would solve the issue, but I cannot find a way to do so as the following will not compile (in Routine, Save will call the first implementation instead of the second):

    public void Routine<T>(T o) {
        var oi = o as IInteresting;
        if (oi != null) {
            this.Save(oi);
        }
    }

    private void Save<T>(T o) where T : new() {
        //Stuff
    }

    private void Save<T>(IInteresting o) {
        //Stuff to convert o to a DTO object with a public parameterless constructor, then call Save(T o)
    }
8
  • 1
    "would not be enough as it would not allow calling the target methods." - actually, it would. ConstructorInfo has an Invoke method. Nov 3, 2013 at 11:59
  • Just use reflection? But first, rethink your design. Nov 3, 2013 at 12:01
  • 2
    What is inside your Save1<T> method?
    – sh1ng
    Nov 3, 2013 at 12:01
  • The Save method is calling a ServiceStack.OrmLite function which requires an object of a type with a parameterless constructor. I need to convert the object to a DTO (respecting : new()) if it is not one already. Nov 3, 2013 at 12:09
  • @O.R.Mapper I don't think ConstructorInfo with its Invoke method would allow me to cast the original object (only create a new one), would it? Nov 3, 2013 at 12:14

3 Answers 3

1

Based on your comments, I imagine you have an object of an unknown type that you want to pass to a generic function that requires the passed-over object to be of the generic type argument, which has to have a parameterless constructor. Hence, for the time being, we can assume that your function Save1<T>(T) from your question is that function, not written by you, not possible to be changed.

A solution to this would be to make the call by using reflection:

2
  • Wow that would do it indeed, though I was hoping for a more out of the box way to achieve this. Nov 3, 2013 at 17:59
  • 1
    @ErwinMayer: As generics invariably - unless you use reflection - have to be resolved at compile-time, while you know your type only at runtime, I'm afraid there is no viable other way. The only slight simplification you could add to this solution is by using a generic class that itself invokes the generic method based on a non-generic method call - that way, you would skip the GetMethod step (which is based on the string-name of the method and thus not compiler-checked) and instead use typeof(MyCallerType<>).MakeGenericType(o.GetType()). Nov 3, 2013 at 18:20
0

Another possible solution, depending on what you do inside private void Save<T>(T o) where T : new() is to use ICloneable interface. Or introduce yours (as I've said, it depends on Save's contents):

interface IConstructible
{
    object Construct();
}

And have:

private void Save1<T>(T o) where T : ICloneable {

Of course this is just a workaround - O. R. Mapper's answer gives the only direct solution.

0
using System.Reflection;


public static class Generics {
 public static void T0<T> ( T obj ) where T : new () {
  Console.WriteLine ( "{0} {1}", obj, obj.GetType () );
 }

 public static void T1<T> ( T obj ) {
  MethodInfo mi = GenericMethodInfo ( typeof ( Generics ), "T0", typeof ( T ) );
  mi.Invoke ( null, new object[] { obj } );
 }

 public static MethodInfo GenericMethodInfo ( Type classType, string methodName, Type genericType ) {
  return classType.GetMethod ( methodName ).MakeGenericMethod ( genericType );
 }
}


Generics.T0 ( 123 );
Generics.T1 ( 123 );
// Impossible.. Generics.T0 ( "12345" );
Generics.T1 ( "12345" );
1
  • The best answers have some explanation as well as code. Please consider adding some context.
    – Chris
    Sep 26, 2014 at 16:55

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.