C# 是我错了还是Roslyn REPL过度保护了?

C# 是我错了还是Roslyn REPL过度保护了?,c#,roslyn,static-constructor,C#,Roslyn,Static Constructor,我试图像LinqPad一样使用Roslyn,但我使用的代码片段是完全有效的C#,并且被告知它们是无效的。 考虑这个BOG标准实用方法。 public static class EnumConvert<TEnum, TUnderlying> where TEnum : struct,IFormattable, IConvertible, IComparable where TUnderlying : struct,IFormattable, IConvertible,

我试图像LinqPad一样使用Roslyn,但我使用的代码片段是完全有效的C#,并且被告知它们是无效的。 考虑这个BOG标准实用方法。
public static class EnumConvert<TEnum, TUnderlying>
    where TEnum : struct,IFormattable, IConvertible, IComparable
    where TUnderlying : struct,IFormattable, IConvertible, IComparable, IComparable<TUnderlying>, IEquatable<TUnderlying>
{
    public static readonly Converter<TEnum, TUnderlying> ToUnderlying;

    public static readonly Converter<TUnderlying, TEnum> ToEnum = 
        Init(out ToUnderlying);

    private static Converter<TUnderlying, TEnum> Init(out Converter<TEnum, TUnderlying> underlier)
    {
        if (Type.GetTypeCode(typeof(TEnum)) != Type.GetTypeCode(typeof(TUnderlying)) || 
            typeof(TEnum) == typeof(TUnderlying))
        {
            throw new ArgumentException("TEnum does not derive from TUnderlying");
        }
        Func<TUnderlying, TUnderlying> Identity = x => x;
        underlier = Delegate.CreateDelegate(typeof(Converter<TEnum, TUnderlying>), Identity.Method) as Converter<TEnum, TUnderlying>;
        return Delegate.CreateDelegate(typeof(Converter<TUnderlying, TEnum>), Identity.Method) as Converter<TUnderlying, TEnum>;
    }
}
公共静态类枚举转换
其中TEnum:struct,IFormattable,IConvertible,IComparable
其中TunderLiving:struct、IFormattable、IConvertible、IComparable、IComparable、IEquatable
{
公共静态只读转换器;
公共静态只读转换器ToEnum=
初始值(向外延伸);
专用静态转换器初始化(输出转换器底层)
{
if(Type.GetTypeCode(typeof(TEnum))!=Type.GetTypeCode(typeof(tunderlieng))|
typeof(TEnum)=typeof(Tunderlieng))
{
抛出新的ArgumentException(“TEnum不是从TunderLiing派生的”);
}
Func Identity=x=>x;
underlier=Delegate.CreateDelegate(typeof(Converter),Identity.Method)作为转换器;
将Delegate.CreateDelegate(typeof(Converter),Identity.Method)作为转换器返回;
}
}
Roslyn声称我调用
ToUnderreling
的out参数是无效的


在您问我为什么不使用静态构造函数之前,我希望确保在类上保留
beforefieldinit
class属性。否则,我将支付每次访问该方法时初始化它的费用。在C中,这被认为是有效的,但Roslyn告诉我,
(6,76):错误CS0199:静态只读字段不能被传递ref或out(在静态构造函数中除外)

看起来您只是在使用CTP版本的Roslyn编译器中遇到了一个错误。Roslyn当前(微软内部)版本的代码没有问题。

谢谢,有消息说下一个CTP版本什么时候发布吗?