zoukankan      html  css  js  c++  java
  • 反射点滴Common


    构造函数加载类集

    public Common()
      {
       SampleAssembly = Assembly.Load("NHClass");          //根据程序集名称得到相应程序集
      }

    简单方法举例

    /// <summary>
      /// 用于存储信息
      /// </summary>
      /// <param name="ht">基本信息列表</param>
      /// <param name="ClassName">完整的类名(包括命名空间和类名)</param>
      /// <returns>新增记录的ID</returns>
      #region 用于存储信息
      public int Save(Hashtable ht,string ClassName)
      {
       int returnvalue = -1;         //返回值,新增记录的ID 
       try
       {
        Type Class = SampleAssembly.GetType(ClassName);     //根据类名得到相应的类
        EntityObject obj = (EntityObject)Activator.CreateInstance(Class);  //得到相应类的实例

        myPropertyInfo = Class.GetProperties(System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Instance);      //由反射得到相应类的属性
        foreach(System.Reflection.PropertyInfo pi in myPropertyInfo)    //对传入的数据和类的属性相比较和赋值
        {
         if(ht.Contains(pi.Name))
         {pi.SetValue(obj,ht[pi.Name],null);}
        }

        obj.Save();           //执行类实例的save方法
       }
       catch(PlException ex)
       {
        if(ex.ErrorType == ErrorTypes.NotUnique)
        {throw new Exception("主键冲突!");}
        else{throw new Exception(ex.Message);}    //返回错误信息
       }
       return returnvalue;
      }
      #endregion

  • 相关阅读:
    程序员偷偷深爱的9个不良编程习惯
    JQuery实现放大镜
    ACM1995
    liubo.im
    Linux中的一些点
    EPOLL使用详解
    Elays'Blog
    c#数据库解析
    codeforces #332 div 2 D. Spongebob and Squares
    类型
  • 原文地址:https://www.cnblogs.com/xh831213/p/326567.html
Copyright © 2011-2022 走看看