CSharp · Console
using System;
using System.Reflection;
public class Program
{
public static void Main()
{
MethodInfo method = typeof(Program).GetMethod("StubMethod", BindingFlags.Public | BindingFlags.Instance);
Type methodReturnType = method.ReturnType;
var typeName = methodReturnType.Name;
var typeFullName = methodReturnType.FullName;
var typeToString = methodReturnType.ToString();
Console.WriteLine(typeName);
Console.WriteLine(typeFullName);
Console.WriteLine(typeToString);
Console.WriteLine();
var currentType = methodReturnType.GetType();
Type prevType = null;
PropertyInfo propInfo = GetPropInfo(currentType);
while(currentType != null && propInfo != null)
{
prevType = currentType;
currentType = currentType.BaseType;
propInfo = GetPropInfo(currentType);
if (propInfo == null)
{
Console.WriteLine("Name declared in " + prevType.FullName);
}
}
}
private static PropertyInfo GetPropInfo(Type type)
{
return type.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
}
public Int32 StubMethod() { return 2; }
}