本文整理汇总了C#中SomeClass.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# SomeClass.GetType方法的具体用法?C# SomeClass.GetType怎么用?C# SomeClass.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SomeClass
的用法示例。
在下文中一共展示了SomeClass.GetType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PerformanceComparison
public void PerformanceComparison()
{
// Demonstrating the performance difference of three approaches to getting the values of properties
const int iterations = 10000000;
var obj = new SomeClass();
var s = new Stopwatch();
// First just go through PropertyInfo.GetValue
Console.Out.Write("PropertyInfo.GetValue: ");
PropertyInfo prop = obj.GetType().GetProperty("Val");
s.Reset();
s.Start();
for (int i = 0; i < iterations; i++)
{
prop.GetValue(obj, null);
}
s.Stop();
Console.Out.WriteLine(s.ElapsedMilliseconds);
// Second, create a delegate to get the property directly from the getter method
Console.Out.Write("Delegate getter: ");
var getMethod = prop.GetGetMethod();
Func<SomeClass, string> typedGetter = (Func<SomeClass, string>)Delegate.CreateDelegate(typeof(Func<SomeClass, string>), null, getMethod);
Func<object, object> delegateGetter = o => typedGetter((SomeClass)o);
s.Reset();
s.Start();
for (int i = 0; i < iterations; i++)
{
delegateGetter(obj);
}
s.Stop();
Console.Out.WriteLine(s.ElapsedMilliseconds);
// Thirdly, create a dynamic method to get the property
Console.Out.Write("IL getter: ");
DynamicMethod dynamicMethod = new DynamicMethod("", typeof(object), new Type[] { typeof(object) }, typeof(SomeClass), true);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Call, getMethod);
if (prop.PropertyType.IsValueType)
{
ilGenerator.Emit(OpCodes.Box, prop.PropertyType);
}
ilGenerator.Emit(OpCodes.Ret);
var ilGetter = (Func<object, object>)dynamicMethod.CreateDelegate(typeof(Func<object, object>));
s.Reset();
s.Start();
for (int i = 0; i < iterations; i++)
{
ilGetter(obj);
}
s.Stop();
Console.Out.WriteLine("IL getter: " + s.ElapsedMilliseconds);
}
示例2: Main
static void Main()
{
SomeClass s = new SomeClass();
Console.WriteLine("Type s: {0}",s.GetType().Name);
}