zoukankan      html  css  js  c++  java
  • 用Activator.CreatInstance动态的创建同一个Assembly的对象

    在正常的工厂类中,我们会有很多的switch case之类的,如何避免他们,我们可以利用.Net的Activator帮我们简单的完成这件事情。目前,经过简单大测试,支持public的构造函数,internal private的都还没有仔细看因为我关心的是public的构造函数。
      研究这个的起因在于,我想用基于配置文件的形式来动态的创建DataAccess对象。
      主要是由于我的构造函数参数是一个自定义的类,不是系统的基本数据类型。昨天测试的时候不知道怎么回事发生了问题,所以,今天早上才写了如下的测试代码,发觉真的很喜欢Snippet Compiler
    using System;
    using System.Reflection;
    using System.Collections;
    namespace FishSky.Testing
    {
    public class DesireSay
    {
       
    public DesireSay()
       
    {
       }

       
    private string mWords;
       
    public string Words
       
    {
         
    getreturn mWords;}
         
    set{ mWords=value;}
       }
       
    }

    public class SayHello
    {
        
    private string mIWannaSay="";
        
    private DesireSay mDesireSay;
        
    private int mInternalInt=-1;
        
    public SayHello()
        
    {
        }

        
    public SayHello(string iWannaSay)
        
    {
           mIWannaSay
    =iWannaSay;
        }

        
    public SayHello(DesireSay desireSay)
        
    {
          mDesireSay
    =desireSay;
        }

        
    internal SayHello(int internalInt)
        
    {
          mInternalInt
    =internalInt;
        }

        
    public string Hello()
        
    {
            
    if(mInternalInt!=-1)
            
    {
               
    return mInternalInt.ToString();
            }

            
    else
            
    {
                
    if(mDesireSay!=null)
                
    {
                
    return mDesireSay.Words;
                }

                
    else
                
    {
                    
    if(mIWannaSay=="")
                        
    return "Hello";
                    
    else
                        
    return mIWannaSay;
                }

            }

        }

        
    }

    public class MyClass
    {
        
    public static void Main()
        
    {
            
    string TypeName="FishSky.Testing.SayHello";
            Type type
    =Type.GetType(TypeName,true);
            SayHello hello
    =(SayHello) Activator.CreateInstance(type);
            WL(
    "SayHello says {0}",hello.Hello());
            Console.WriteLine(
    "Next Test with string parameter in constructor..");
            SayHello hello2
    =(SayHello) Activator.CreateInstance(type,new Object[]{"I wanna say Hello!"});
            WL(
    "SayHello says {0}",hello2.Hello());
            Console.WriteLine(
    "Next Test with Class parameter in constructor..");
            DesireSay desireSay
    =new DesireSay();
            desireSay.Words
    ="I Desire say!Let me say!";
            SayHello hello3
    =(SayHello) Activator.CreateInstance(type,new Object[]{desireSay});
            WL(
    "SayHello says {0}",hello3.Hello());
    //        Console.WriteLine("Next Test with int parameter in internal constructor..");
    //        SayHello hello4=(SayHello) Activator.CreateInstance(type,new Object[]{250});
    //        //SayHello hello4=(SayHello)Activator.CreateInstance(type,BindingFlags.NonPublic,null,new object[250],new CultureInfo());
    //        WL("SayHello says {0}",hello4.Hello());        
            RL();
        }

        
        
    Helper methods
    }

    }

  • 相关阅读:
    10种 分布式ID生成方式(新增MongoDB的ObjectId)
    Spring核心接口Ordered的实现及应用 (动态切换数据源时候用到)
    No module named 'Crypto' 解决方案
    使用Anaconda管理多个版本的Python环境
    深入浅出Blazor webassembly 之API服务端保护
    [转载]HTTPS 是如何保护你的安全的
    [转载]api接口token的生成和应用
    深入浅出Blazor webassembly之HttpClient使用
    深入浅出Blazor webassembly之自定义Input组件
    深入浅出Blazor webassembly之EditForm
  • 原文地址:https://www.cnblogs.com/wildfish/p/317060.html
Copyright © 2011-2022 走看看