當前位置: 首頁>>代碼示例>>C#>>正文


C# Activator.CreateInstance方法代碼示例

本文整理匯總了C#中System.Activator.CreateInstance方法的典型用法代碼示例。如果您正苦於以下問題:C# Activator.CreateInstance方法的具體用法?C# Activator.CreateInstance怎麽用?C# Activator.CreateInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Activator的用法示例。


在下文中一共展示了Activator.CreateInstance方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      // Initialize array of characters from a to z.
      Char[] chars = new Char[26]; 
      for (int ctr = 0; ctr < 26; ctr++)
         chars[ctr] = (char) (ctr + 0x0061);

      Object obj = Activator.CreateInstance(typeof(String),
                                            new Object[] { chars, 13, 10 } );
      Console.WriteLine(obj);                                          
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:17,代碼來源:Activator.CreateInstance

輸出:

nopqrstuvw

示例2: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      char[] characters = { 'a', 'b', 'c', 'd', 'e', 'f' };
      object[][] arguments = new object[3][] { new object[] { characters },
                                               new object[] { characters, 1, 4 },
                                               new object[] { characters[1], 20 } };

      for (int ctr = 0; ctr <= arguments.GetUpperBound(0); ctr++) {
         object[] args = arguments[ctr];
         object result = Activator.CreateInstance(typeof(String), args);
         Console.WriteLine("{0}: {1}", result.GetType().Name, result);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:19,代碼來源:Activator.CreateInstance

輸出:

String: abcdef
String: bcde
String: bbbbbbbbbbbbbbbbbbbb

示例3: Person

//引入命名空間
using System;

public class Person
{
   private string _name;
   
   public Person()
   { }
   
   public Person(string name)
   {
      this._name = name;
   }
   
   public string Name
   { get { return this._name; }
     set { this._name = value; } }
   
   public override string ToString()
   {
      return this._name;
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:24,代碼來源:Activator.CreateInstance

示例4: Main

//引入命名空間
using System;
using System.Runtime.Remoting;

public class Example
{
   public static void Main()
   {
      ObjectHandle handle = Activator.CreateInstance("PersonInfo", "Person");
      Person p = (Person) handle.Unwrap();
      p.Name = "Samuel";
      Console.WriteLine(p);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:Activator.CreateInstance

輸出:

Samuel

示例5: Activator.CreateInstance(Type classType)

//引入命名空間
using System;
using System.Reflection;
using System.IO;

public class MainClass
{
  public static int Main(string[] args)
  {
    Assembly a = null;
    try
    {
      a = Assembly.Load("YourLibraryName");
    }
    catch(FileNotFoundException e)
    {Console.WriteLine(e.Message);}
  
    Type classType = a.GetType("YourLibraryName.ClassName");

    object obj = Activator.CreateInstance(classType);
  
    MethodInfo mi = classType.GetMethod("MethodName");

    mi.Invoke(obj, null);

    object[] paramArray = new object[2];    
    paramArray[0] = "Fred";
    paramArray[1] = 4;
    mi = classType.GetMethod("MethodName2");
    mi.Invoke(obj, paramArray);

    return 0;
  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:34,代碼來源:Activator.CreateInstance

示例6: Main

//引入命名空間
using System;
using System.Reflection;
using System.Runtime.Remoting;

public class Example
{
   public static void Main()
   {
      ObjectHandle handle = Activator.CreateInstance("PersonInfo", "Person");
      Object p = handle.Unwrap();
      Type t = p.GetType();
      PropertyInfo prop = t.GetProperty("Name");
      if (prop != null)
         prop.SetValue(p, "Samuel");

      MethodInfo method = t.GetMethod("ToString");
      Object retVal = method.Invoke(p, null); 
      if (retVal != null)
         Console.WriteLine(retVal);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:22,代碼來源:Activator.CreateInstance

輸出:

Samuel

示例7: Main

//引入命名空間
using System;

class DynamicInstanceList
{
    private static string instanceSpec = "System.EventArgs;System.Random;" +
        "System.Exception;System.Object;System.Version";

    public static void Main()
    {
        string[] instances = instanceSpec.Split(';');
        Array instlist = Array.CreateInstance(typeof(object), instances.Length);
        object item;
        for (int i = 0; i < instances.Length; i++)
        {
            // create the object from the specification string
            Console.WriteLine("Creating instance of: {0}", instances[i]);
            item = Activator.CreateInstance(Type.GetType(instances[i]));
            instlist.SetValue(item, i);
        }
        Console.WriteLine("\nObjects and their default values:\n");
        foreach (object o in instlist)
        {
            Console.WriteLine("Type:     {0}\nValue:    {1}\nHashCode: {2}\n",
                o.GetType().FullName, o.ToString(), o.GetHashCode());
        }
    }
}

// This program will display output similar to the following:
//
// Creating instance of: System.EventArgs
// Creating instance of: System.Random
// Creating instance of: System.Exception
// Creating instance of: System.Object
// Creating instance of: System.Version
//
// Objects and their default values:
//
// Type:     System.EventArgs
// Value:    System.EventArgs
// HashCode: 46104728
//
// Type:     System.Random
// Value:    System.Random
// HashCode: 12289376
//
// Type:     System.Exception
// Value:    System.Exception: Exception of type 'System.Exception' was thrown.
// HashCode: 55530882
//
// Type:     System.Object
// Value:    System.Object
// HashCode: 30015890
//
// Type:     System.Version
// Value:    0.0
// HashCode: 1048575
開發者ID:.NET開發者,項目名稱:System,代碼行數:58,代碼來源:Activator.CreateInstance

示例8: new

public static T Factory<T>() where T : new()
{
    return new T();
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:Activator.CreateInstance


注:本文中的System.Activator.CreateInstance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。