本文整理汇总了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);
}
}
输出:
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);
}
}
}
输出:
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;
}
}
示例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);
}
}
输出:
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;
}
}
示例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);
}
}
输出:
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
示例8: new
public static T Factory<T>() where T : new()
{
return new T();
}