我关心使用枚举键的通用词典.
如下页所述,使用键的枚举将分配内存:http: //blogs.msdn.com/b/shawnhar/archive/2007/07/02/twin-paths-to-garbage-collector-nirvana.aspx
我已经测试并确认了这个行为,这导致我的项目出现问题.为了便于阅读,我认为使用枚举键是非常有用的,对我来说最好的解决方案是编写一个实现的类IDictionary<TKey, TValue>,它将在内部使用整数键.原因是我不想更改所有现有的词典以使用整数作为键,并进行隐式转换.这将是最好的性能,但它最初会给我很多工作,它会降低可读性.
所以我尝试了几种方法,包括使用GetHashCode(不幸的是分配内存)来构建内部Dictionary<int, TValue>.
所以,把它包装在一个问题中; 任何人都可以想到一个我可以使用的解决方案,以保持可读性Dictionary<SomeEnum, TValue>,同时具有一个Dictionary<int, TValue>?
任何建议都非常感谢.
有没有办法将泛型类型限制为 an enum,类似于以下内容?
class MyClass<T extends enum> {}
Run Code Online (Sandbox Code Playgroud)
喜欢的东西在C# 。
如果我有一个带有这样的struct约束的泛型接口:
public interface IStruct<T> where T : struct { }
Run Code Online (Sandbox Code Playgroud)
我可以提供枚举作为我的类型T,因为enum满足struct约束:
public class EnumIsAStruct : IStruct<DateTimeKind> { }
Run Code Online (Sandbox Code Playgroud)
C#7.3增加了一个Enum约束.以下代码以前是非法的,现在编译:
public class MCVE<T> : IStruct<T> where T : struct, Enum { }
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是,以下内容无法编译:
public class MCVE<T> : IStruct<T> where T : Enum { }
Run Code Online (Sandbox Code Playgroud)
......有错误
CS0453类型'T'必须是非可空值类型才能在泛型类型或方法'IStruct'中将其用作参数'T'
为什么是这样?我希望受限制的泛型类型Enum可用作类型参数,其中类型受约束struct但似乎不是这种情况 - 我必须将Enum约束更改为struct, Enum.我的期望是错的吗?
我一直在尝试创建一个扩展方法,它可以在任何枚举上工作,以返回它的值.
而不是这样做:
Enum.GetValues(typeof(BiasCode)).Cast<BiasCode>()
Run Code Online (Sandbox Code Playgroud)
这样做会很好:
new BiasCode().Values()
Run Code Online (Sandbox Code Playgroud)
如果没有新的话会更好,但这是另一个问题.
我有一个.NET小提琴,它有一个接近的解决方案(代码如下所示).此代码的问题是扩展方法正在返回List<int>.我想让它返回枚举值本身的列表.回归List<int>并不可怕; 这只是意味着我必须施展结果.
甚至可以这样做吗?我尝试使扩展方法通用,但遇到了问题.这与我能够得到的一样接近:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
foreach (int biasCode in new BiasCode().Values())
{
DisplayEnum((BiasCode)biasCode);
}
}
public static void DisplayEnum(BiasCode biasCode)
{
Console.WriteLine(biasCode);
}
}
public enum BiasCode
{
Unknown,
OC,
MPP
}
public static class EnumExtensions
{
public static List<int> Values(this Enum theEnum)
{
var enumValues = new List<int>();
foreach (int enumValue …Run Code Online (Sandbox Code Playgroud) 我正在使用的数据库目前有一个varchar字段,在我的代码中我想将潜在的值映射到枚举,如:
public enum UserStatus
{
Anonymous,
Enrolled,
SuperUser
}
Run Code Online (Sandbox Code Playgroud)
在此列的数据库级别,对其进行约束,其值必须为:
ANONYMOUS
ENROLLED
SUPERUSER
Run Code Online (Sandbox Code Playgroud)
我有可能这样做:
UserStatus.SuperUser.ToString()
Run Code Online (Sandbox Code Playgroud)
并且这个价值是超级的,这是一致的,而不是搞砸了吗?
我有一个类"许可证",它是一堆枚举标志的集合,如下所示:
Public class License
{
UsageType Usage { get; set; }
PlatformType Platform { get; set; }
public enum UsageType { read = 1, write = 2, wipe = 4, all = 7 }
public enum PlatformType { windows = 1, linux = 2, ios = 4, all = 7 }
etc...
}
Run Code Online (Sandbox Code Playgroud)
关键是可以将相同类别的各种标志一起进行"或"运算,以形成用户可以对所述许可证执行的操作的配置文件.现在我试图以人性化的方式显示"Usage"和"Platform"的值,例如,如果Usage == UsageType.read | UsageType.write然后它应该被解析为"读,写".
我通过测试每个标志的值并为每个标志附加enumitem.ToString()为一个字符串来成功地使用单个枚举类型.既然我有很多这些枚举和价值,我想提出一个更通用的方法.
我提出了这个(下面),但由于我不熟悉c#中的模板函数所以我不知道为什么这不起作用,但至少它应该说明我的意思:
private string parseEnum<T>(T value)
{
string ret = "";
foreach (var ei in (T[])Enum.GetValues(typeof(T)))
{
if (value.HasFlag(ei)) ret += ei.ToString() …Run Code Online (Sandbox Code Playgroud) 我现在非常接近了解Generics(我认为).
但是,只是认为System.Enum不容易实现为泛型类型.我有这门课:
public class Button<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable {
public TEnum Identifier {
get;
private set; //Set in the ctor
}
}
Run Code Online (Sandbox Code Playgroud)
和
public abstract class AbstractInputDevice<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable {
private List<Button<TEnum>> _buttons = new List<Button<TEnum>>();
public Button<TEnum> GetButton(TEnum Identifier){
foreach(Button<TEnum> button in _buttons){
if(button.Identifier == Identifier) //<- compiler throws
return button;
}
Debug.Log("'" + GetType().Name + "' cannot return an <b>unregistered</b> '" + typeof(Button<TEnum>).Name + "' that listens to '" …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
class Base<T>
{
//Base members
}
Run Code Online (Sandbox Code Playgroud)
我希望通用T是一个枚举(如果可能的话使用约束).我怎么能在C#中做到这一点?
编辑:
使用代码契约 - 由Akash Kava介绍 - 似乎也是一个很好的方式.我设法让它产生运行时错误,这是无用的. 这是我试过的代码.应该可以生成编译时警告但我无法使其工作.
我正在编写一些枚举功能,并具有以下功能:
public static T ConvertStringToEnumValue<T>(string valueToConvert, bool isCaseSensitive)
{
if (typeof(T).BaseType.FullName != "System.Enum" && typeof(T).BaseType.FullName != "System.ValueType")
{
throw new ArgumentException("Type must be of Enum and not " + typeof (T).BaseType.FullName);
}
if (String.IsNullOrWhiteSpace(valueToConvert))
return (T)typeof(T).TypeInitializer.Invoke(null);
valueToConvert = valueToConvert.Replace(" ", "");
if (typeof(T).BaseType.FullName == "System.ValueType")
{
return (T)Enum.Parse(Nullable.GetUnderlyingType(typeof(T)), valueToConvert, !isCaseSensitive);
}
return (T)Enum.Parse(typeof(T), valueToConvert, !isCaseSensitive);
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
EnumHelper.ConvertStringToEnumValue<Enums.Animals?>("Cat");
Run Code Online (Sandbox Code Playgroud)
我现在想要将T的约束添加到Enum,例如(我从Stackoverflow文章中获得):where T : struct, IConvertible但是我遇到了问题,因为T需要能够获取可以为空的枚举.错误消息说:
类型'Enums.Animals?' 必须是非可空值类型才能在泛型类型或方法中将其用作参数"T"
有没有办法做到这一点,或者我需要依靠运行时检查我在方法内部?
谢谢大家!
可能的重复:
创建将 T 限制为枚举的通用方法
给定一个仅对枚举值进行操作的通用方法
static void <T> method(T enum) where T ?????
{
// do something with enum...
}
Run Code Online (Sandbox Code Playgroud)
如何限制T只接受枚举值?我试过使用struct但是这不允许使用可以为空的枚举类型调用我的方法。
c# ×9
enums ×6
generics ×5
.net ×1
c#-7.3 ×1
comparison ×1
constraints ×1
dart ×1
dictionary ×1
enumeration ×1
flutter ×1
mono ×1
nullable ×1
parsing ×1
performance ×1