C# - 创建 EnumRandomizer 类

发布于 2024-11-28 06:42:32 字数 1286 浏览 2 评论 0原文

我对如何正确地做到这一点感到困惑。我发现这个并且不确定这是否可以是重复的。

我将从头开始。我能够通过创建一个专门用于随机化该枚举的类来实现我发布的线程中给出的建议,如下所示:

public class Foo
{
    public static Random rand = new Random();
    public static FooEnum[] values;
    public static FooEnum GetRandomFoo()
    {
       values = (FooEnum[]) Enum.GetValues(typeof(FooEnum));
       return values[rand.Next(0, Values.Length)];      
    }

    public enum FooEnum { A, B, C}
}

这工作得很好。唯一的问题是,它显然导致我在枚举上包装了一个类,因此总是要求我想出两种不同的方式来表达同一件事(例如,类 Gender 包装在枚举 Sex 上;类 Weapon 包装在枚举 WeaponType 上,等等) .);

这也是低效的,因为它需要我编写必须为同一件事一遍又一遍地重新实现的代码。因此,显而易见的解决方案是创建一个 Random Enum 类,它可以采用任何类型的枚举类型并使用以下方法对其进行随机化。

有人对如何做到这一点有任何想法吗?我的方法不太有效:

public class EnumRandomizer 
    {

    private Random rand;
    private Type[] values;
    private int randomizerId;

    private static int randomizerCount = 0;

    public EnumRandomizer ()
    {
        rand = new Random();
        randomizerId = randomizerCount;
        randomizerCount++;

    }

    public Type RandomEnum(Type type)
    {
        values = (Type[]) Enum.GetValues(type);

        return values[rand.Next(0, values.Length)];
    }
}

有人接受吗?

I'm stumped as about how to do this correctly. I found this and am not sure as to whether or not this could be a duplicate.

I'll start from the beginning. I was able to implement the suggestion given in the thread I posted by creating a class specifically for randomizing that enum, which looked like the following:

public class Foo
{
    public static Random rand = new Random();
    public static FooEnum[] values;
    public static FooEnum GetRandomFoo()
    {
       values = (FooEnum[]) Enum.GetValues(typeof(FooEnum));
       return values[rand.Next(0, Values.Length)];      
    }

    public enum FooEnum { A, B, C}
}

This worked quite well. The only problem was that it obviously resulted in me wrapping a class over my enum and therefore always required me to come up with two different ways to say the same thing (e.g. class Gender wraps over enum Sex; class Weapon wraps over enum WeaponType, etc.);

This was also inefficient because it would require me to write code which had to be reimplemented over and over for the same thing. Therefore, the obvious solution to this is to create a Random Enum class which can take any kind of enum type and randomize it using the following method.

Does anyone have any ideas on how to do this? My method isn't quite working:

public class EnumRandomizer 
    {

    private Random rand;
    private Type[] values;
    private int randomizerId;

    private static int randomizerCount = 0;

    public EnumRandomizer ()
    {
        rand = new Random();
        randomizerId = randomizerCount;
        randomizerCount++;

    }

    public Type RandomEnum(Type type)
    {
        values = (Type[]) Enum.GetValues(type);

        return values[rand.Next(0, values.Length)];
    }
}

Any takers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-12-05 06:42:32
public class EnumRandomizer
{
    public static Random rand = new Random();

    public static T GetRandomValue<T>()
    {
        T[] values = (T[])(Enum.GetValues(typeof(T)));
        return values[rand.Next(0, values.Length)];
    }
}

用法:

public class Main
{
    public enum FooEnum { A, B, C }

    public static void Main(string[] args)
    {
        // Note that since the method does not take an argument which
        // specifies the generic type, you must provide it explicitly.
        FooEnum randomFoo = EnumRandomizer.GetRandomValue<FooEnum>();
    }
}

至于为什么您的原始版本不起作用:实际解决的是 System.Type 类,而不是您传入的类型。泛型可以让您以您尝试的方式动态分配类型。这就是类型安全操作需要它们的原因。

public class EnumRandomizer
{
    public static Random rand = new Random();

    public static T GetRandomValue<T>()
    {
        T[] values = (T[])(Enum.GetValues(typeof(T)));
        return values[rand.Next(0, values.Length)];
    }
}

Usage:

public class Main
{
    public enum FooEnum { A, B, C }

    public static void Main(string[] args)
    {
        // Note that since the method does not take an argument which
        // specifies the generic type, you must provide it explicitly.
        FooEnum randomFoo = EnumRandomizer.GetRandomValue<FooEnum>();
    }
}

As far as why your original isn't working: What is actually resolving is the System.Type class, not the type you pass in. Generics are what let you dynamically assign a type in the manner you are attempting. That's why they are required for type-safe operation.

眉黛浅 2024-12-05 06:42:32

制作通用方法

public static T GetRandomFoo<T>()
{
    Random rand = new Random();
    T[] values = (T[])Enum.GetValues(typeof(T));
    return values[rand.Next(0, values.Count)];
}

或者这是您的类的正确版本,不建议使用,因为它会导致

    public class EnumRandomizer
    {

        private Random rand;
        private IList values;
        private int randomizerId;

        private static int randomizerCount = 0;

        public EnumRandomizer()
        {
            rand = new Random();
            randomizerId = randomizerCount;
            randomizerCount++;

        }

        public object RandomEnum(Type type)
        {
            values = Enum.GetValues(type);
            return values[rand.Next(0, values.Count)];
        }
    }

你可以像这样使用它

   FooEnum f = (FooEnum)new EnumRandomizer().RandomEnum(typeof(FooEnum));

希望这有帮助

Make generic method

public static T GetRandomFoo<T>()
{
    Random rand = new Random();
    T[] values = (T[])Enum.GetValues(typeof(T));
    return values[rand.Next(0, values.Count)];
}

Or this is correct version of you class which is not recommended to use, because it causes boxing/unboxing

    public class EnumRandomizer
    {

        private Random rand;
        private IList values;
        private int randomizerId;

        private static int randomizerCount = 0;

        public EnumRandomizer()
        {
            rand = new Random();
            randomizerId = randomizerCount;
            randomizerCount++;

        }

        public object RandomEnum(Type type)
        {
            values = Enum.GetValues(type);
            return values[rand.Next(0, values.Count)];
        }
    }

you can use it like this

   FooEnum f = (FooEnum)new EnumRandomizer().RandomEnum(typeof(FooEnum));

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文