我的项目的架构(三)

TranContext是一个比较重要的类,在这个类中,使用了反射方法,实现了根据配置文件动态创建类,实现了接口的作用.

 1     public abstract class ConfigurationFactory
 2     {
 3         public ConfigurationFactory()
 4         {
 5             //
 6             // TODO: 在此处添加构造函数逻辑
 7             //
 8         }
 9         public static object CreateObject(object context,Type type)
10         {
11             ArgumentValidation.CheckForNullReference (context,"TranContext");
12             ArgumentValidation.CheckForNullReference (type,"Type");
13             ConstructorInfo constructor = type.GetConstructor(new Type[] {context.GetType ()});
14             if (constructor == null)
15             {
16                 //throw new Exception(SR.ExceptionProviderMissingConstructor(type.FullName));
17             }
18             object createdObject = null;
19             try
20             {
21                 createdObject = constructor.Invoke(new object[]{context});
22             }
23             catch
24             {
25 //                Console.WriteLine(ex.Message );
26 //                throw new Exception ("create oject: "+type.FullName+" failed!");
27                 throw;
28             }
29             return createdObject;
30         }
31         public static object CreateObject(object context,string typeName)
32         {
33             ArgumentValidation.CheckForNullReference (context,"TranContext");
34             ArgumentValidation.CheckForEmptyString(typeName,"typeName");
35             object createObject=null;
36             try
37             {
38                 Assembly _Assembly=Assembly.Load(Assembly.GetExecutingAssembly().FullName);
39                 createObject=_Assembly.CreateInstance(typeName,
40                     true,
41                     System.Reflection.BindingFlags.CreateInstance,
42                     null,
43                     new object[]{context},//args
44                     null,//CultureInfo
45                     null);
46             }
47             catch
48             {
49 //                Console.WriteLine (ex.Message);
50 //                throw new Exception ("create oject: "+typeName+" failed!");
51                 throw;
52             }
53             return createObject;
54         } 
55     }

 

这两个方法,实现了相同的目标,创建了一个对象,所不同的是实现的方法不同,第一个方法需要一个指定的类型作为参数,创建的对象所指定的类型,它使用了.net中ConstructorInfo这个类来实现的.

 

ConstructorInfo constructor = type.GetConstructor(new Type[] {context.GetType ()});

createdObject = constructor.Invoke(new object[]{context});

第一句创建了各个构造器,第二句则创建了这个对象. Invoke里的参数是创建的对象的构造函数所要的参数

 

第二个方法,不需要创建对象的类型,而只需要对象的名称就可以了.在这个方法中使用的是Assembly这个类.

Assembly _Assembly=Assembly.Load(Assembly.GetExecutingAssembly().FullName);

这条语句得到当前运行的Assembly

createObject=_Assembly.CreateInstance(typeName,

                       true,

                        System.Reflection.BindingFlags.CreateInstance,

                       null,

                       new object[]{context},//args

                        null,//CultureInfo

                       null);

这条语句则创建了指定的对象.

这个函数里的参数比较多.下面是引用msdn中:

public object CreateInstance(
   string bool BindingFlags Binder object[] CultureInfo object[] );

 

参数
typeName

要查找的类型的 Type.FullName。

ignoreCase
如果为 true,则忽略类型名的大小写;否则,为false

bindingAttr

影响执行搜索的方式的位屏蔽。此值是BindingFlags中的位标志的组合。

binder

一个对象,它启用绑定、参数类型的强制、成员的调用以及通过反射进行MemberInfo 对象的检索。如果 binder 为空引用(Visual Basic 中为 Nothing),则使用默认联编程序。 args

Object 类型的数组,包含要传递给构造函数的参数。此参数数组在数量、顺序和类型方面必须与要调用的构造函数的参数匹配。如果需要默认的构造函数,则 args 必须是空数组或空引用(Visual Basic 中为 Nothing)。

culture

用于控制类型强制的 CultureInfo 的实例。如果这是空引用(Visual Basic 中为 Nothing),则使用当前线程的 CultureInfo。(例如,这对于将表示 1000 的 String 转换为 Double 值是必需的,因为不同的区域性以不同的方式表示 1000。)

activationAttributes

Object 类型的数组,包含一个或多个可以参与激活的激活属性。激活属性的一个示例是:

URLAttribute(http://hostname/appname/objectURI)

返回值

表示此类型且匹配指定条件的 Object 的实例;如果没有找到typeName,则为空引用(Visual Basic 中为 Nothing)。
我的本意是想通过一个工厂方法来实现接口的动态实现.但是利用了.net框架中的反射机制,发现要达到我原来的目的这么容易就实现了.所以就没有使用原来的工厂方法来实现,通过下面简单的函数调用即可:

1         public IBuilder CreateBuilder()
2         {
3             if (null==section)
4                 readConfigurationFile();
5             return (IBuilder)ConfigurationFactory.CreateObject(this,section.Builder.ToString());
6         }

读取配置文件是通过函数readConfigurationFile()来实现的

 1         /// <summary>
 2         /// 取得配置文件
 3         /// </summary>
 4         private void readConfigurationFile()
 5         {
 6             try
 7             {
 8                 System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
 9                 string SourcePath=Path.GetDirectoryName(Asm.Location);
10                 SourcePath+=System.Configuration.ConfigurationSettings.AppSettings["MaunExcelConfig"];
11                 section=XmlHelper.Read (setting.Key,SourcePath) as ManuSetting;
12             }
13             catch(System.IO.IOException ex)
14             {
15                 throw new System.Configuration.ConfigurationException(SR.MessageConfigFileNotFound,ex);
16             }
17             catch(Exception ex)
18             {
19                 throw new System.Configuration.ConfigurationException(SR.MessageConfigFileNotValidOrSectionNotFound,ex);
20             }
21         }

其中最主要的是section=XmlHelper.Read (setting.Key,SourcePath) as ManuSetting;这句

XmlHelper是我仿造微软的Configuration Application Block做的一个配置类.功能是可以把一个类序列化和反序列化.这里就不多说了,不熟悉的朋友可以到http://www.cnblogs.com/Terrylee/archive/2006/08/01/Enterprise_Library.html 这里去查看,有很详细的说明.

完整源代码:

  1     public class TranContext:IDisposable
  2     {
  3         private readonly TranSetting setting=null;
  4         private IBuilder builder=null;
  5         private ILog log=null;
  6         private ManuSetting section=null;
  7         public event EndReportEventHandler EndReport;
  8         public TranContext()
  9         {
 10             //
 11             // TODO: 在此处添加构造函数逻辑
 12             //
 13         }
 14         public TranContext(TranSetting setting)
 15         {
 16             ArgumentValidation.CheckForNullReference (setting,"TranSetting");
 17             this.setting =setting;
 18         }
 19         public TranContext(string key,string askFileName,string operation)
 20         {
 21             ArgumentValidation.CheckForEmptyString (key,"key");
 22             ArgumentValidation.CheckForEmptyString (askFileName,"askFileName");
 23             ArgumentValidation.CheckForEmptyString (operation,"operation");
 24             setting=new TranSetting (this,key,askFileName,operation);
 25         }
 26         public TranSetting Setting
 27         {
 28             get{return this.setting;}
 29         }
 30         public IBuilder Builder
 31         {
 32             get
 33             {
 34                 if (null==builder)
 35                     builder=CreateBuilder();
 36                 return builder ;
 37             }
 38         }
 39         public ILog Log
 40         {
 41             get
 42             {
 43                 if (null==log)
 44                     log=CreateLog();
 45                 return log;
 46             }
 47             set{log=value;}
 48         }
 49         public IEntity Entity
 50         {
 51             get{return (IEntity)section.Entity;}
 52         }
 53         public ManuSetting Section
 54         {
 55             get
 56             {
 57                 if (null==section)                    
 58                 {
 59                     readConfigurationFile();
 60                 }
 61                 return section;
 62             }
 63             set{section=value;}
 64         }
 65         public string ReturnFileName
 66         {
 67             get{return setting.ReturnFileName;}
 68         }
 69         /// <summary>
 70         /// 取得配置文件
 71         /// </summary>
 72         private void readConfigurationFile()
 73         {
 74             try
 75             {
 76                 System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
 77                 string SourcePath=Path.GetDirectoryName(Asm.Location);
 78                 SourcePath+=System.Configuration.ConfigurationSettings.AppSettings["MaunExcelConfig"];
 79                 section=XmlHelper.Read (setting.Key,SourcePath) as ManuSetting;
 80             }
 81             catch(System.IO.IOException ex)
 82             {
 83                 throw new System.Configuration.ConfigurationException(SR.MessageConfigFileNotFound,ex);
 84             }
 85             catch(Exception ex)
 86             {
 87                 throw new System.Configuration.ConfigurationException(SR.MessageConfigFileNotValidOrSectionNotFound,ex);
 88             }
 89         }
 90         public void Execute()
 91         {
 92             using(ContextWatch watch=new ContextWatch (this))
 93             {
 94                 watch.EndReport +=new EndReportEventHandler(OnEndReport);
 95                 watch.ExecuteContext();
 96             }
 97         }
 98         public void Dispose()
 99         {
100 //            Dispose(true);
101             GC.SuppressFinalize(this);
102         }
103 //        private void Dispose(bool disposing)
104 //        {
105 //            if (disposing)
106 //            {
107 //                if (this.watch != null)
108 //                {
109 //                    this.watch.EndReport -= new EndReportEventHandler (OnEndReport);
110 //                    watch.Dispose();
111 //                }
112 //            }
113 //        }
114         public IBuilder CreateBuilder()
115         {
116             if (null==section)
117                 readConfigurationFile();
118             return (IBuilder)ConfigurationFactory.CreateObject(this,section.Builder.ToString());
119         }
120         public ILog CreateLog()
121         {
122             if (null==section)
123                 readConfigurationFile();
124             return (ILog)ConfigurationFactory.CreateObject(this,section.Log.ToString());
125         }
126         public ITran CreateTran(object detail)
127         {
128             if (null==section)
129                 readConfigurationFile();
130             return (ITran)ConfigurationFactory.CreateObject(detail,section.Tran.ToString());
131         }
132         private void OnEndReport(object sender,EndReportEventArgs e )
133         {
134             if (EndReport != null)
135             {
136                 EndReport(this, e);
137             }
138         }
139 
140     }

转载于:https://www.cnblogs.com/nerozhang/archive/2007/05/15/746860.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380344.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1的个数

描述 小南刚学了二进制&#xff0c;他想知道一个数的二进制表示中有多少个1&#xff0c;你能帮他写一个程序来完成这个任务吗&#xff1f; 输入 第一行输入一个整数N&#xff0c;表示测试数据的组数(1< N<1000) 每组测试数据只有一行&#xff0c;是一个整数M(0< M…

单片机编程文件组织形式(个人编程规范)

1、外设或系统资源驱动函数组织形式。所有函数写在.c文件里面&#xff0c;.c最前面包含自身头文件。每个.c文件都有一个相对应的.h文件&#xff0c;其他文件或系统只调用.h文件。 2、.c文件除了最前面要包含自身头文件外&#xff0c;应该尽量全部是函数定义&#xff0c;接口信息…

计算机在我国开始被应用于,计算机应用推动自动化与信息化的发展

计算机应用推动自动化与信息化的发展【摘要】本文简单介绍了我国计算机应用的发展情况&#xff0c;分析了计算机应用同自动化和信息化之间的关系&#xff0c;通过计算机应用在我国各领域中的运用&#xff0c;来体现计算机应用对自动化和信息化的推动作用。【关键词】计算机&…

cobaltstrike生成一个原生c,然后利用xor加密解密执行

首先cobaltstrike生成一个原生c&#xff0c;我的是&#xff1a; /* length: 797 bytes */ unsigned char buf[] "\xfc\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b\x52\x30\x8b\x52\x0c" "\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff\x31\xc0\xac…

tolowercase_JavaScript中的String toLowerCase()方法与示例

tolowercase字符串toLowerCase()方法 (String toLowerCase() Method) toLowerCase() Method is a string method in JavaScript, it is used to converts all alphabets in lowercase and returns the new string with lowercase alphabets. toLowerCase()方法是JavaScript中的…

这样就可以很方便的知道明天的天气了

今天在侧边栏加了一个实用的小东西——天气预报。它可以根据来访者的ip地址自动判断地区&#xff0c;并展现今天以及明天的天气预报。这样来看blog的时候就可以知道什么时候该去收衣服啦&#xff5e;哈哈&#xff01;实现代码其实很简单。就是套一个IFRAME&#xff0c;里面套个…

队花的烦恼一

描述 ACM队的队花C小经常抱怨&#xff1a;“C语言中的格式输出中有十六、十、八进制输出&#xff0c;然而却没有二进制输出&#xff0c;哎&#xff0c;真遗憾&#xff01;谁能帮我写一个程序实现输入一个十进制数n&#xff0c;输出它的二进制数呀&#xff1f;” 难道你不想帮…

计算机数学基础模拟试题,计算机数学基础》模拟考试试题.doc

PAGE / NUMPAGES《计算机数学基础(2)》模拟试题(1)一、单项选择题(每小题3分&#xff0c;共15分)1. 数值x*的近似值x0.121510-2&#xff0c;若满足( )&#xff0c;则称x有4位有效数字。A. B.C. D.2.设矩阵&#xff0c;那么以A为系数矩阵的线性方程组AXb的雅可比迭代矩阵为( )。…

压缩矩阵

压缩矩阵&#xff1a;指为多个值相同的元素只分配一个存储空间&#xff0c;对零元素不分配存储空间特殊矩阵&#xff1a;指具有许多相同矩阵元素或零元素&#xff0c;并且这些相同矩阵元素或零元素的分配有一定规律性 1、对称矩阵 对称矩阵&#xff1a;矩阵每个元素都有aijaj…

线性方程组 python_线性方程组的表示 使用Python的线性代数

线性方程组 pythonPrerequisites: 先决条件&#xff1a; Defining a Vectors 定义向量 Defining a Matrix 定义矩阵 In this article, we are going to learn how to represent a linear equation in Python using Linear Algebra. For example we are considering an equatio…

初步体验数据驱动之美---TreeView

1.前言继上一篇《WPF应用基础篇---TreeView》的发布之后&#xff0c;有部分朋问我关于里面一些基础应用的问题&#xff0c;可能是我写得不够详细&#xff0c;所以在这里&#xff0c;我想再次那文章中的案例来谈谈初步体验数据驱动之美&#xff0c;摆脱旧WinForm编程习惯(靠触发…

c#2.0匿名方法五(转)

在循环控制结构内使用匿名方法的局部变量的用法   当处理循环控制结构时将局部变量封装入类的数据成员有着有趣但危险的一面&#xff0c;让我们看看下面代码&#xff1a;public class Program{ public delegate void MyDelegate(); public static void Main(string[] args)…

不可以!

描述 判断&#xff1a;两个数x、y的正负性。 要求&#xff1a;不可以使用比较运算符&#xff0c;即”<”,”>”,”<”,”>”,””,”!”。 输入 有多组数据&#xff0c;每组数据占一行&#xff0c;每一行两个数x&#xff0c;y。 x、y保证在int范围内。 输出 …

树的基本概念

0x01 树 树&#xff1a;n个结点的有限集合&#xff0c;n0&#xff0c;空树任何非空树只有一个根结点n个结点的树只有n-1条边&#xff08;除根结点&#xff0c;每个结点只有一个前驱&#xff0c;一个前驱一条边&#xff0c;根据这个算的&#xff09;有序树与无序树&#xff1a;…

制作两个字符串字谜的最小步骤数

Prerequisite: 先决条件&#xff1a; Hashing data structure 散列数据结构 Problem statement: 问题陈述&#xff1a; Find the minimum number of steps to make two strings Anagram. Both strings are of the same length and the lower case. At each step, you can con…

小学计算机教学教师培训,例谈小学信息技术课堂的有效教学

例谈小学信息技术课堂的有效教学在社会的各个领域&#xff0c;大家都不可避免地会接触到论文吧&#xff0c;论文可以推广经验&#xff0c;交流认识。为了让您在写论文时更加简单方便&#xff0c;以下是小编整理的例谈小学信息技术课堂的有效教学的论文相关内容&#xff0c;供大…

关于varchar2在pl/sql和schema级别的最大值

http://www.cublog.cn/u/30637/showart_1919003.html http://www.itpub.net/thread-1216548-1-1.html  转载于:https://www.cnblogs.com/wzc998/archive/2011/08/12/2136406.html

业务工作流平台设计(九)

自定义审核活动 前面已经讲了许多有关自定义活动在设计上需要注意的一些事项&#xff0c;但对于自定义审核活动来讲&#xff0c;我们的设计还要有许多工作要进行。 为了简化用户的流程上的设计将流程的一些算法封装到自定义活动中可以大大增加自定义活动的使用的方便性。其直接…

国王的魔镜

描述 国王有一个魔镜&#xff0c;可以把任何接触镜面的东西变成原来的两倍——只是&#xff0c;因为是镜子嘛&#xff0c;增加的那部分是反的。 比如一条项链&#xff0c;我们用AB来表示&#xff0c;不同的字母表示不同颜色的珍珠。如果把B端接触镜面的话&#xff0c;魔镜会把…

C和汇编---数组

0x01 初始化数组 1、没有初始化数组 #include "stdio.h" int main(void) {int data[4];for (int i0;i<4;i){printf("%d\t",data[i]);}return 0; }不同系统&#xff0c;输出结果可能不一样&#xff1a; 反汇编&#xff1a; 4: int data[4]; 5…