本文整理匯總了C#中System.Activator類的典型用法代碼示例。如果您正苦於以下問題:C# Activator類的具體用法?C# Activator怎麽用?C# Activator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Activator類屬於System命名空間,在下文中一共展示了Activator類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: DoSomething
//引入命名空間
using System;
using System.Reflection;
using System.Text;
public class SomeType
{
public void DoSomething(int x)
{
Console.WriteLine("100 / {0} = {1}", x, 100 / x);
}
}
public class Example
{
static void Main()
{
// Create an instance of the StringBuilder type using
// Activator.CreateInstance.
Object o = Activator.CreateInstance(typeof(StringBuilder));
// Append a string into the StringBuilder object and display the
// StringBuilder.
StringBuilder sb = (StringBuilder) o;
sb.Append("Hello, there.");
Console.WriteLine(sb);
// Create an instance of the SomeType class that is defined in this
// assembly.
System.Runtime.Remoting.ObjectHandle oh =
Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase,
typeof(SomeType).FullName);
// Call an instance method defined by the SomeType type using this object.
SomeType st = (SomeType) oh.Unwrap();
st.DoSomething(5);
}
}
輸出:
Hello, there. 100 / 5 = 20