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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:53:07+00:00 2026-06-13T02:53:07+00:00

I have classes like below. The TestOne method is running but the TestTwo is

  • 0

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);
        }
    }
}
  • 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-06-13T02:53:08+00:00Added an answer on June 13, 2026 at 2:53 am

    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<>?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a lot of helper classes for some reasons like below:
I am not sure if the title is accurate. I have several classes like
I have got several classes looking like the one below, and I need to
Assume you have several arbitrary classes like below: class Foo { $foovar; public function
Can I do something like the below where I can have multiple classes trigger
I have a database return result which has flatten results like below. I want
I have 2 classes like below. public class Quest { public int ID{ get;set;
Why can't open generic types be passed as parameters. I frequently have classes like:
I have classes structured like this: Public MustInherit Class A ' several properties End
Supposed I have two classes like this: public class Parent { public string Id

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.