本文整理汇总了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