0

以下のようなクラスがあります。TestOne メソッドは実行されていますが、TestTwo は実行されていません。TestTwo にはパラメーターがあるためです。アクティベーターはエラーを出します。この理由を解決するにはどうすればよいですか?Activator.CreateInstance と 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);
        }
    }
}
4

2 に答える 2

1

Activator.CreateInstance を使用すると、指定したパラメーターに最適なコンストラクターを見つけようとします。パラメーターを指定しない場合、Foo にはないパラメーターのないコンストラクターが検索されます。

Foo には文字列引数を持つコンストラクタしかないため、次のように CreateInstance に同じ定義を提供する必要があります。

Activator.CreateInstance(typeof(Foo), new object[] { "a string" });
于 2012-10-17T19:02:34.060 に答える
0

Lazy のコンストラクター情報については、こちらを参照してください。

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

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

Func<IEnumerable<Foo>>を作成するために、 の型のデリゲートをそのコンストラクターに送信する必要がありますLazy<T>。しかし、ここでは のタイプのデリゲートを送信しようとしていますFunc<int, IEnumerable<Foo>>

の型のデリゲートを作成するFunc<IEnumerable<Foo>>ことはできTestMethod2ません。ここにあります。

Func<>の代わりにDelegate.CreateDelegateを使用してデリゲートを定義する方法は?

于 2012-10-19T13:27:45.433 に答える