English 中文(简体)
选择一个班级的随机子级
原标题:Choose a random subclass of a class

附录一有一个类别<代码>Letter 和许多类别<代码>A,B, ...... 页: 1

我如何选择<代码>的随机儿童 页: 1

我知道,这是很宽的,但我想的是这样的东西。

public Letter GetRandomLetter()
{
    //return a random instance of A, B or any child class
}

儿童班级数量不详,因此,我只能使用一些随机数字的逻辑,以恢复每个儿童班级的榜样。 我也不想进行反思。

假设有数千个儿童班级,如果我一年后回来,想要创造新的儿童班级,那么,为了保持正确工作,就不应只写上班级本身。

问题回答

这里是你想要做的事情的最简单办法。

var factories = new Func<Letter>[]
{
    () => new A(),
    () => new B(),
    () => new C(),
};

Letter letter = factories[Random.Shared.Next(factories.Length)]();

建立<代码>List<Type>,附有所有信函,并使用<代码>Random类别从中随机选择。 如果您需要该信的事例,则会创造新的一封信,或如果不是的话,将返回类型改为<代码>。 类型

www.un.org/chinese/ga/president

页: 1 我说了一些问题,因为我相对较新的问题(只有几个月的时间),但我认为这将奏效。

Here’s an example letter class

class Letter 
{
    public static List<Letter> Letters = new();

    static Letter() 
   {
        Type type = typeof(this);
        if (type != typeof(new Letter())
        {
             Letters.Add(type);
        }
   }
}

public Letter GetLetter() 
{
    Random random = new();
    return new Letter.Letters[random choice];
}

由于收集了<条码>Func<Letter>,称随机功能很方便。 但是,你如何根据集会的类型建立这些职能的收集?

对于这些类型的问题,我认为有必要写一份通用的助手职能,然后通过思考将其称作“;”

private static Func<Letter> GetDelegate<T>() where T : Letter, new()
    => () => new T();

var getDelegate = new Func<Func<Letter>>(GetDelegate<Letter>)
    .Method
    .GetGenericMethodDefinition();

factories = typeof(Letter).Assembly.GetTypes()
    .Where(t => t != typeof(Letter) 
        && !t.IsAbstract 
        && t.IsAssignableTo(typeof(Letter)))
    .Select(t => (Func<Letter>)getDelegate
        .MakeGenericMethod(t)
        .Invoke(null, null))
    .ToArray();

虽然这种办法确实使用了反思,但成本是一次性的。 在一个静态领域储存收集工作,使用这些方法应当作为手工界定的收集工作效率。

If you need to pass a standard set of arguments to a constructor, I would lean on static abstract members

public interface ILetter<T> where T : ILetter<T>
{
    static abstract T Factory(int argument);
}

private static Func<int, Letter> GetDelegate<T>() where T : Letter, ILetter<T>
    => (arg) => T.Factory(arg);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签