Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么以下两种方法生成相同的IL? 公共静态类扩展 { 公共静态T Include(此System.Enum类型,T值),其中T:struct { 返回((T)(ValueType)((int)(ValueType)类型|(int)(ValueType)值)); } 公共静态T Include1(此System.Enum类型,T值) { 返回((T)(对象)((int)(对象)类型|(int)(对象)值)); } }_C#_Il - Fatal编程技术网

C# 为什么以下两种方法生成相同的IL? 公共静态类扩展 { 公共静态T Include(此System.Enum类型,T值),其中T:struct { 返回((T)(ValueType)((int)(ValueType)类型|(int)(ValueType)值)); } 公共静态T Include1(此System.Enum类型,T值) { 返回((T)(对象)((int)(对象)类型|(int)(对象)值)); } }

C# 为什么以下两种方法生成相同的IL? 公共静态类扩展 { 公共静态T Include(此System.Enum类型,T值),其中T:struct { 返回((T)(ValueType)((int)(ValueType)类型|(int)(ValueType)值)); } 公共静态T Include1(此System.Enum类型,T值) { 返回((T)(对象)((int)(对象)类型|(int)(对象)值)); } },c#,il,C#,Il,如果你看到为这两种方法生成的IL,它们看起来是一样的,还是我遗漏了什么。。。 为什么对第一个Include方法进行装箱?ValueType是一种引用类型。诚实的只有当它是T时,它才是一个结构。您需要将所有的ValueType替换为T,这样它就不会出现框。但是,将不会有从T到int的内置强制转换。。。所以:你不能。你得打拳击。另外,并不是所有的枚举都是基于int(例如,对于enum Foo:ushort,框为enum,取消框为int将失败) 在C#4.0中,dynamic可能是一种厚颜无耻的方式:

如果你看到为这两种方法生成的IL,它们看起来是一样的,还是我遗漏了什么。。。
为什么对第一个Include方法进行装箱?

ValueType
是一种引用类型。诚实的只有当它是
T
时,它才是一个结构。您需要将所有的
ValueType
替换为
T
,这样它就不会出现框。但是,将不会有从
T
int
的内置强制转换。。。所以:你不能。你得打拳击。另外,并不是所有的枚举都是基于
int
(例如,对于
enum Foo:ushort
,框为enum,取消框为int将失败)

在C#4.0中,
dynamic
可能是一种厚颜无耻的方式:

 public static class Extensions
{
    public static T Include<T>(this System.Enum type,T value) where T:struct 
    {
      return ((T) (ValueType) (((int) (ValueType) type | (int) (ValueType) value)));

    }
    public static T Include1<T>(this System.Enum type, T value) 
    {
        return ((T)(object)((int)(object)type | (int)(object)value));

    }
}

您应该添加IL…ValueType是一个类。在这两种方法中,您从一个值类型转换为一个类,因此,它们都将引用框括起来。(更新了答案,提供了一些这样做的选项)我认为这不会起作用。它会说没有从
int
T
@CodeInChaos的转换-是的,我意识到在按“提交”时-按F5
public static T Include<T>(this T type, T value) where T : struct
{
    return ((dynamic)type) | value;
}
static void Main()
{
    var both = Test.A.Include(Test.B);
}
enum Test : ulong
{
    A = 1, B = 2
}

public static T Include<T>(this T type, T value) where T : struct
{
    return DynamicCache<T>.or(type, value);
}
static class DynamicCache<T>
{
    public static readonly Func<T, T, T> or;
    static DynamicCache()
    {
        if(!typeof(T).IsEnum) throw new InvalidOperationException(typeof(T).Name + " is not an enum");
        var dm = new DynamicMethod(typeof(T).Name + "_or", typeof(T), new Type[] { typeof(T), typeof(T) }, typeof(T),true);
        var il = dm.GetILGenerator();
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Or);
        il.Emit(OpCodes.Ret);
        or = (Func<T, T, T>)dm.CreateDelegate(typeof(Func<T, T, T>));
    }
}