C#泛型函数访问静态抽象成员调试时显示System.BadImageFormatException
泛型函数中访问静态抽象属性的调试异常问题
此前曾提出过相关问题,本次为后续问题。
我在泛型函数中尝试访问接口定义的静态抽象属性,泛型函数代码如下:
public static T AsType<T>(...) where T : ICode { //... if(InterfaceCode != T.InterfaceCode) return null; if(SubtypeCode != T.SubtypeCode) return null; //... }
对应的接口定义:
public interface ICode { public static abstract ushort SubtypeCode { get; } public static abstract ushort InterfaceCode { get; } }
为了使用静态抽象接口成员特性,我已将项目从.NET Core 3.1升级至.NET 7。
目前遇到的问题:代码运行时无异常,但在VS2022调试环境中查看T.InterfaceCode时,会显示以下异常:
'T.InterfaceCode' threw an exception of type 'System.BadImageFormatException'
可复现代码示例:
internal class Program { static void Main(string[] args) { AsType<Test>(1, 3); } public static T AsType<T>(ushort InterfaceCode, ushort SubtypeCode) where T : ICode { //... if (InterfaceCode != T.InterfaceCode) return default(T); if (SubtypeCode != T.SubtypeCode) return default(T); //... return default(T); } public interface ICode { public static abstract ushort SubtypeCode { get; } public static abstract ushort InterfaceCode { get; } } public class Item : ICode { public static ushort SubtypeCode { get; } = 1; public static ushort InterfaceCode { get; } = 2; } public class Test : Item { public static new ushort InterfaceCode { get; } = 1; public static new ushort SubtypeCode { get; } = 3; } }
内容的提问来源于stack exchange,提问作者MP9
相关产品推荐
相关产品推荐

