一个动态获取XML值的父类

本文介绍了一种使用C#反射机制自动生成XML处理类实例的方法。通过定义特定的属性标记,可以解析XML节点并映射到类属性上。该方法支持不同类型的XML节点,并能根据这些节点创建对应的类实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;
using System.Xml;

namespace ebsoft.Xml.Factory
{
    public abstract class XmlClassBase
    {
        protected System.Xml.XmlNode _data;
    }

    public enum NodeValueType
    {
        AttributeValue,
        NodeInnerXml,
        Node
    }

    public class NodeValueAttribute : Attribute
    {
        private NodeValueType _Type = NodeValueType.AttributeValue;
        public NodeValueType Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
            }
        }
        private string _NodeName;
        public string NodeName
        {
            get
            {
                return _NodeName;
            }
            set
            {
                _NodeName = value;
            }
        }
        public NodeValueAttribute()
        {

        }
        public NodeValueAttribute(string nodeName, NodeValueType type)
        {
            _Type = type;
            _NodeName = nodeName;
        }
        public NodeValueAttribute(NodeValueType type)
        {
            _Type = type;
        }
    }

    public class XmlClassFactory<T>
    {
        private static Type _CreatedType;
        public static T CreateInstance(XmlNode Data)
        {
            if (_CreatedType == null)
                _CreatedType = CreateClass();
            return (T)Activator.CreateInstance(_CreatedType, Data);
        }
        private static Type CreateClass()
        {
            Type t = typeof(T);
            //if (t.IsGenericType)
            //    throw new Exception("T必须不能为泛型");
            if (!t.IsSubclassOf(typeof(XmlClassBase)))
                throw new Exception("T必须从XmlClassBase派生");

            string name = "XmlHelper.Factory." + t.Name;
            AssemblyName asmName = new AssemblyName(name);
            AppDomain domain = AppDomain.CurrentDomain;
            AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(name);
            //AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
            //ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(name, "s.dll");
            TypeBuilder typeBuilder = moduleBuilder.DefineType(name, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed, t, Type.EmptyTypes);

            PropertyInfo[] pis = t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            foreach (PropertyInfo pi in pis)
            {
                MethodInfo miget = pi.GetGetMethod(true);
                if (!miget.IsAbstract)
                    continue;
                MethodInfo miset = pi.GetSetMethod(true);

                object[] FieldAttribute = pi.GetCustomAttributes(typeof(NodeValueAttribute), false);
                
                string FieldName = "";
                if (FieldAttribute.Length == 0)
                {
                    throw new Exception(string.Format("抽象属性{0}必须标识NodeValueAttribute属性", pi.Name));
                }

                NodeValueAttribute fa = (NodeValueAttribute)FieldAttribute[0];

                if (pi.PropertyType.FullName != "System.String" && fa.Type != NodeValueType.Node)
                {
                    throw new Exception(string.Format("抽象属性{0}被标识为{1},因此他的类型必须定义为String", pi.Name , fa.Type));
                }

                if (fa.Type == NodeValueType.Node && pi.PropertyType.FullName != "System.Xml.XmlNode")
                {
                    throw new Exception(string.Format("抽象属性{0}被标识为{1},因此他的类型必须定义为XmlNode", pi.Name , fa.Type));
                }

                if (fa.NodeName == null)
                {
                    FieldName = pi.Name;
                }
                else
                {
                    FieldName = fa.NodeName;
                }

                PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(pi.Name, PropertyAttributes.HasDefault, pi.PropertyType, Type.EmptyTypes);

                MethodAttributes getMethodAttribute = MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual;
                if (miget.IsFamily)
                    getMethodAttribute = getMethodAttribute | MethodAttributes.Family;
                else
                    getMethodAttribute = getMethodAttribute | MethodAttributes.Public;
                MethodBuilder getPropertyBuilder = typeBuilder.DefineMethod("get_" + pi.Name, getMethodAttribute, pi.PropertyType, Type.EmptyTypes);
                ILGenerator getIL = getPropertyBuilder.GetILGenerator();
                switch (fa.Type)
                {
                    case NodeValueType.AttributeValue:
                        getIL.Emit(OpCodes.Ldarg_0);
                        getIL.Emit(OpCodes.Ldfld, t.BaseType.GetField("_data", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
                        getIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_Attributes", BindingFlags.Instance | BindingFlags.Public));
                        getIL.Emit(OpCodes.Ldstr, FieldName);
                        getIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlAttributeCollection).GetMethod("get_ItemOf", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string) } , null));
                        getIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_Value", BindingFlags.Public | BindingFlags.Instance));
                        getIL.Emit(OpCodes.Ret);
                        break;
                    case NodeValueType.NodeInnerXml:
                        getIL.Emit(OpCodes.Ldarg_0);
                        getIL.Emit(OpCodes.Ldfld, t.BaseType.GetField("_data", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
                        getIL.Emit(OpCodes.Ldstr, FieldName);
                        getIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string) } , null));
                        getIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_InnerXml", BindingFlags.Public | BindingFlags.Instance));
                        getIL.Emit(OpCodes.Ret);
                        break;
                    case NodeValueType.Node:
                        getIL.Emit(OpCodes.Ldarg_0);
                        getIL.Emit(OpCodes.Ldfld, t.BaseType.GetField("_data", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
                        getIL.Emit(OpCodes.Ldstr, FieldName);
                        getIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string) } , null));
                        getIL.Emit(OpCodes.Ret);
                        break;
                    default:
                        break;
                }
                propertyBuilder.SetGetMethod(getPropertyBuilder);

                if (miset == null)
                {
                    continue;
                }
                MethodAttributes setMethodAttribute = MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual;
                if (miset.IsFamily)
                    setMethodAttribute = setMethodAttribute | MethodAttributes.Family;
                else
                    setMethodAttribute = setMethodAttribute | MethodAttributes.Public;
                MethodBuilder setPropertyBuilder = typeBuilder.DefineMethod("set_" + pi.Name, setMethodAttribute, null, new Type[] { typeof(string) });
                ILGenerator setIL = setPropertyBuilder.GetILGenerator();
                switch (fa.Type)
                {
                    case NodeValueType.AttributeValue:
                        setIL.Emit(OpCodes.Ldarg_0);
                        setIL.Emit(OpCodes.Ldfld, t.BaseType.GetField("_data", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
                        setIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_Attributes", BindingFlags.Instance | BindingFlags.Public));
                        setIL.Emit(OpCodes.Ldstr, FieldName);
                        setIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlAttributeCollection).GetMethod("get_ItemOf", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string) } , null));
                        setIL.Emit(OpCodes.Ldarg_1);
                        setIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("set_Value", BindingFlags.Public | BindingFlags.Instance));
                        setIL.Emit(OpCodes.Ret);
                        break;
                    case NodeValueType.NodeInnerXml:
                        setIL.Emit(OpCodes.Ldarg_0);
                        setIL.Emit(OpCodes.Ldfld, t.BaseType.GetField("_data", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
                        setIL.Emit(OpCodes.Ldstr, FieldName);
                        setIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string) } , null));
                        setIL.Emit(OpCodes.Ldarg_1);
                        setIL.Emit(OpCodes.Callvirt, typeof(System.Xml.XmlNode).GetMethod("set_InnerXml", BindingFlags.Public | BindingFlags.Instance));
                        setIL.Emit(OpCodes.Ret);
                        break;
                    case NodeValueType.Node:
                        break;
                    default:
                        break;
                }
                propertyBuilder.SetSetMethod(setPropertyBuilder);

            }

            ConstructorBuilder cb = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[] { typeof(System.Xml.XmlNode) });
            ILGenerator cbil = cb.GetILGenerator();
            cbil.Emit(OpCodes.Ldarg_0);
            cbil.Emit(OpCodes.Ldarg_1);
            cbil.Emit(OpCodes.Stfld, t.BaseType.GetField("_data", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
            cbil.Emit(OpCodes.Ldarg_0);
            cbil.Emit(OpCodes.Call, t.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Type.EmptyTypes, null));
            cbil.Emit(OpCodes.Ret);

            Type t1 = typeBuilder.CreateType();
            //assemblyBuilder.Save("s.dll");

            return t1;
        }
    }
}


用法

 public abstract class HOME : XmlClassBase
    {
        /// <summary>
        /// <para>-1001                               未登录</para>
        /// <para>-31011  现在系统繁忙,稍后再试。</para>
        /// </summary>
        [NodeValue]
        public abstract string code { get;}
        [NodeValue]
        public abstract string message { get;}
    }

  return XmlClassFactory<需要付值的类>.CreateInstance("XML节点");

### Spring 工具类获取 ApplicationContext 对象 在 Spring 框架中,`ApplicationContext` 是核心接口之一,提供了访问应用上下文中各种 Bean 的功能。为了方便开发者更灵活地获取 `ApplicationContext` 实例,通常可以借助工具类来实现这一目标。 以下是几种常见的通过工具类获取 `ApplicationContext` 的方式: #### 方法一:继承 `ApplicationObjectSupport` 通过让工具类继承 `org.springframework.context.ApplicationObjectSupport` 抽象类,并将其标记为组件 (`@Component`) 以便被 Spring 容器管理,在容器启动时会自动调用父类的 `setApplicationContext()` 方法完成注入[^1]。之后可通过重载的方法 `getApplicationContext()` 获得当前的应用上下文实例。 ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationObjectSupport; import org.springframework.stereotype.Component; @Component public class ApplicationContextUtil extends ApplicationObjectSupport { /** * 获取Spring的ApplicationContext对象 */ public static ApplicationContext getApplicationContext() { return applicationContextHolder.get(); } private static final ThreadLocal<ApplicationContext> applicationContextHolder = new ThreadLocal<>(); @Override protected void initApplicationContext() throws BeansException { super.initApplicationContext(); applicationContextHolder.set(getApplicationContext()); } } ``` #### 方法二:自定义静态变量存储 另一种常见的方式是在工具类内部声明一个静态成员变量保存 `ApplicationContext` 参考引用[^2]。当 Spring 初始化完成后,利用事件监听机制或者手动设置该字段即可供后续使用。 ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class CustomApplicationContext implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static Object getBean(String beanName){ return context.getBean(beanName); } } ``` #### 方法三:模拟实现 ApplicationContext 接口 对于某些特殊场景下可能需要自行扩展或简化版的功能支持,则可以根据实际需求重新设计并实现自己的 `ApplicationContext` 类型结构[^3]。不过这种方式较少见于生产环境之中,更多适用于学习研究目的。 关于可能出现 NullPointerException 错误的情况分析如下: 如果按照上述任一种方案操作仍遇到空指针异常问题,除了确认 Servlet 部分逻辑无误外,还需仔细核查是否存在 XML 文件路径拼接错误、命名空间未正确定义等问题导致依赖注入失败情形发生[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值