Why do I get an error 'Activator.CreateInstance can't find the constructor' while create Lazy<>?

StackOverflow https://stackoverflow.com/questions/12941343

  •  08-07-2021
  •  | 
  •  

Question

I have classes like below. The TestOne method is running but the TestTwo is not running. Because the TestTwo has a parameter. The Activator gives an error. How can I solve this reason ? I need to use together Activator.CreateInstance and Delegate.CreateDelegate ?

public class Foo
{
    public Foo(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
}

public class MyClass
{
    public Lazy<IEnumerable<Foo>> Foos { get; set; }
}

[TestFixture]
public class Test
{
    private IEnumerable<T> CreateItems<T>() where T : class
    {
        for (int i = 0; i < 5; i++)
        {
            yield return (T)Activator.CreateInstance(typeof(T), i.ToString(CultureInfo.InvariantCulture));
        }
    }

    private IEnumerable<T> CreateItems<T>(int count) where T : class
    {
        for (int i = 0; i < count; i++)
        {
            yield return (T)Activator.CreateInstance(typeof(T), i.ToString(CultureInfo.InvariantCulture));
        }
    }

    public IEnumerable<T> TestMethod<T>() where T : class
    {
        return CreateItems<T>();
    }

    public IEnumerable<T> TestMethod2<T>(int i) where T : class
    {
        return CreateItems<T>(i);
    }

    [Test]
    public void TestOne()
    {
        var u = new MyClass();
        var prop = u.GetType().GetProperties().First(x => x.PropertyType.IsGenericType 
            && x.PropertyType.GetGenericTypeDefinition() == typeof(Lazy<>));
        var enu = prop.PropertyType.GetGenericArguments()[0];
        var j = enu.GetGenericArguments()[0];
        var func = typeof(Func<>).MakeGenericType(enu);
        var mi = this.GetType().GetMethod("TestMethod").MakeGenericMethod(j);
        var factory = Delegate.CreateDelegate(func, this, mi);
        var type = typeof(Lazy<>).MakeGenericType(enu);
        prop.SetValue(u, Activator.CreateInstance(type, factory), null);
        Debug.WriteLine("Count:" + u.Foos.Value.Count());
        foreach (var foo in u.Foos.Value)
        {
            Debug.WriteLine(foo.Name);
        }
    }

    [Test]
    public void TestTwo()
    {
        var u = new MyClass();
        var prop = u.GetType().GetProperties().First(x => x.PropertyType.IsGenericType 
            && x.PropertyType.GetGenericTypeDefinition() == typeof(Lazy<>));
        var enu = prop.PropertyType.GetGenericArguments()[0]; 
        var j = enu.GetGenericArguments()[0];
        var func = typeof(Func<,>).MakeGenericType(typeof(int), enu);
        var mi = this.GetType().GetMethod("TestMethod2").MakeGenericMethod(j);
        var factory = Delegate.CreateDelegate(func, this, mi);
        var type = typeof(Lazy<>).MakeGenericType(enu);
        // How do I send parameter to the TestMethos2 ? 
        // I get the error this line.
        prop.SetValue(u, Activator.CreateInstance(type, factory), null);
        Debug.WriteLine("Count:" + u.Foos.Value.Count());
        foreach (var foo in u.Foos.Value)
        {
            Debug.WriteLine(foo.Name);
        }
    }
}
Was it helpful?

Solution 2

Look at here for the constructor info of Lazy.

http://msdn.microsoft.com/en-us/library/dd642331.aspx

public Lazy<IEnumerable<Foo>> Foos { get; set; }

Must be sent a delegate of type of Func<IEnumerable<Foo>> to its constructor for creating Lazy<T>. But in here trying to send a delegate of type of Func<int, IEnumerable<Foo>>.

Creating a delegate of type of Func<IEnumerable<Foo>> for the TestMethod2 is imposible. It is in here.

How to define a delegate using Delegate.CreateDelegate instead of Func<>?

OTHER TIPS

When you use Activator.CreateInstance, it will try to find the best constructor for the parameters you supply it. If you provide no parameters, it will search for a parameter-less constructor, which Foo doesn't have.

Since Foo only have a constructor with a string argument, you must provide the same definition to CreateInstance, like this:

Activator.CreateInstance(typeof(Foo), new object[] { "a string" });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top