using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using AspectCore.Extensions.Reflection;
namespace Grpc.Extension.Common
{
///
/// ReflectorExtensions
///
public static class ReflectorExtensions
{
///
/// 获取属性值
///
///
///
///
///
///
public static T GetPropertyValue(this object obj, string name, BindingFlags bindingFlags) where T : class
{
var chProperty = obj.GetType().GetTypeInfo().GetProperty(name, bindingFlags);
if (chProperty == null) throw new InvalidOperationException($"Cannot locate property {name}");
var chReflector = chProperty.GetReflector();
var value = chReflector.GetValue(obj) as T;
return value;
}
///
/// 获取字段值
///
///
///
///
///
///
public static Tuple GetFieldValue(this object obj, string name, BindingFlags bindingFlags) where T : class
{
var chField = obj.GetType().GetTypeInfo().GetField(name, bindingFlags);
if (chField == null) throw new InvalidOperationException($"Cannot locate field {name}");
var chReflector = chField.GetReflector();
var value = chReflector.GetValue(obj) as T;
return Tuple.Create(value, chField);
}
///
/// 获取字段值
///
///
///
///
///
///
public static T GetFieldValue(this Type type, string name, BindingFlags bindingFlags) where T : class
{
var chField = type.GetTypeInfo().GetField(name, bindingFlags);
if (chField == null) throw new InvalidOperationException($"Cannot locate field {name}");
var chReflector = chField.GetReflector();
var value = chReflector.GetValue(null) as T;
return value;
}
///
/// 获取方法
///
///
///
///
///
public static MethodInfo GetMethodInfo(this Type type, string name, BindingFlags bindingFlags)
{
var method = type.GetMethod(name, bindingFlags);
if(method == null) throw new InvalidOperationException($"Cannot locate method {name}");
return method;
}
}
}