You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#继承类静态构造器异常问题及优化方案咨询

静态构造器触发时机问题与多格式数据对象设计优化

背景

负责管理测试设备的项目,涉及约10万份XML配置文件,需转换为Excel、Access等格式便于更新。原VBA代码处理速度极慢,改用C#重构,已完成XML Schema用于读取XML,现需构建可复用数据对象管理多格式读写,通过派生类定义不同XML类型的数据内容。

原设计与问题

设计了双层数据结构,但静态构造器表现不符合预期,代码如下:

public abstract class Data_Structure {
    private static readonly Dictionary<string, int> _DatIndex = new Dictionary<string, int>();
    private static string _Name = "";
    private string[] _Data = null;
    protected static void Init(string datName, List<string> listProps) {
        int iIndex = 1;
        _Name = datName;
        foreach (string iProp in listProps) {
            _DatIndex.Add(iProp, iIndex);
            iIndex++;
        }
    }
    public Data_Structure() {
        _Data = new string[_DatIndex.Count + 1];
    }
    public static Dictionary<string, int> Get_PropList => _DatIndex;
    public static string Get_Name => _Name; 
    public string Prop_Get(string Prop_Name) {
        return this._Data[_DatIndex[Prop_Name]];
    }
    public void Prop_Set(string Prop_Name, string Prop_Value) {
        this._Data[_DatIndex[Prop_Name]] = Prop_Value;
    }
    // Code to manage input/output
}
public class Data_Item : Data_Structure {
    static Data_Item() {
        List<string> listProps = new List<string>();
        PropertyInfo[] arryProps = typeof(Data_Item).GetProperties();
        foreach (PropertyInfo iProp in arryProps) {
            listProps.Add(iProp.Name);
        }
        Init("Data_Item_Name", listProps);
    }
    public string Property1 {
        get => this.Prop_Get("Property1");
        set => this.Prop_Set("Property1", value);
    }
    // More Properties...
}

设计思路:Data_Structure作为不同I/O格式的统一接口,每种XML文件类型对应一个派生类(如Data_Item)定义属性,以便使用BindingList<T>等功能。

但调用时出现异常:

public void testFunction() {
    Console.WriteLine(Data_Item.Get_PropList.Count); //Outputs 0
    Data_Item tempItem = new Data_Item();
    Console.WriteLine(Data_Item.Get_PropList.Count); //Outputs correct number of properties
}

首次访问Data_Item.Get_PropList.Count输出0,实例化对象后才输出正确的属性数量。

需求

对反射不太熟悉,但清楚反射运行时性能差,希望提前完成反射操作以提升10万文件遍历的效率,同时希望得到简化派生类定义的建议。

补充解决方案

发现访问父类静态属性时仅触发父类静态构造器,通过泛型找到了解决方案,代码如下:

public abstract class Data_Item<TChild> where TChild : Data_Item<TChild> {
    private static Data_Schema _CurSchema;
    static Data_Item() {
        _CurSchema = Data_Schema_Libarary.Get(typeof(TChild));
    }
}
public class Data_Item_Instance : Data_Item<Data_Item_Instance> {
    [Custom_Property(Attributes1)]
    public string Property1 {get; set;}
    [Custom_Property(Attributes2)]
    public string Property2 {get; set;}
}
public static class Data_Schema_Library {
    private static Dictionary<string, Data_Schema> _SchemaLibrary;
    public static Data_Schema Get<T>() {
        //Read T with Reflection and load a Data_Schema into/from _SchemaLibrary
    }
}
public class Data_Schema {
    public Custom_Property Attribute {get; set;}
}

该方案虽仍存在性能问题,但已有改善,分享供参考。


内容的提问来源于stack exchange,提问作者Manfred ad. Rickenbocker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 08:09:53