From 3e3731297fb51156f013dcc836a1ce286a718944 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Thu, 11 May 2017 16:55:03 +0800 Subject: [PATCH] refactor. --- .../Extensions/ReflectionExtensions.cs | 162 ------------------ 1 file changed, 162 deletions(-) delete mode 100644 src/Cap.Consistency/Extensions/ReflectionExtensions.cs diff --git a/src/Cap.Consistency/Extensions/ReflectionExtensions.cs b/src/Cap.Consistency/Extensions/ReflectionExtensions.cs deleted file mode 100644 index 773381c..0000000 --- a/src/Cap.Consistency/Extensions/ReflectionExtensions.cs +++ /dev/null @@ -1,162 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Threading.Tasks; - -namespace Cap.Consistency.Extensions -{ - public static class ReflectionExtensions - { - public static Type MakeDefType(this TypeInfo byRefTypeInfo) - { - if (byRefTypeInfo == null) - { - throw new ArgumentNullException(nameof(byRefTypeInfo)); - } - if (!byRefTypeInfo.IsByRef) - { - throw new ArgumentException($"Type {byRefTypeInfo} is not passed by reference."); - } - - var assemblyQualifiedName = byRefTypeInfo.AssemblyQualifiedName; - var index = assemblyQualifiedName.IndexOf('&'); - assemblyQualifiedName = assemblyQualifiedName.Remove(index, 1); - - return byRefTypeInfo.Assembly.GetType(assemblyQualifiedName, true); - } - - public static bool CanInherited(this TypeInfo typeInfo) - { - if (typeInfo == null) - { - throw new ArgumentNullException(nameof(typeInfo)); - } - - if (!typeInfo.IsClass || typeInfo.IsSealed) - { - return false; - } - - if (typeInfo.IsNested) - { - return typeInfo.IsNestedPublic && typeInfo.DeclaringType.GetTypeInfo().IsPublic; - } - else - { - return typeInfo.IsPublic; - } - } - - internal static MethodInfo GetMethodBySign(this TypeInfo typeInfo, MethodInfo method) - { - if (method.IsGenericMethod) - { - foreach (var genericMethod in typeInfo.DeclaredMethods.Where(m => m.IsGenericMethod)) - { - if (method.ToString() == genericMethod.ToString()) - { - return genericMethod; - } - } - } - - return typeInfo.GetMethod(method.Name, method.GetParameterTypes()); - } - - public static Type[] GetParameterTypes(this MethodInfo method) - { - if (method == null) - { - throw new ArgumentNullException(nameof(method)); - } - return method.GetParameters().Select(parame => parame.ParameterType).ToArray(); - } - - public static bool IsPropertyBinding(this MethodInfo method) - { - if (method == null) - { - throw new ArgumentNullException(nameof(method)); - } - - return method.GetBindingProperty() != null; - } - - public static PropertyInfo GetBindingProperty(this MethodInfo method) - { - if (method == null) - { - throw new ArgumentNullException(nameof(method)); - } - - foreach (var property in method.DeclaringType.GetTypeInfo().DeclaredProperties) - { - if (property.CanRead && property.GetMethod == method) - { - return property; - } - - if (property.CanWrite && property.SetMethod == method) - { - return property; - } - } - - return null; - } - - public static MethodInfo GetMethod(Expression expression) - { - if (expression == null) - { - throw new ArgumentNullException(nameof(expression)); - } - var methodCallExpression = expression.Body as MethodCallExpression; - if (methodCallExpression == null) - { - throw new InvalidCastException("Cannot be converted to MethodCallExpression"); - } - return methodCallExpression.Method; - } - - public static MethodInfo GetMethod(string name) - { - if (name == null) - { - throw new ArgumentNullException(nameof(name)); - } - - return typeof(T).GetTypeInfo().GetMethod(name); - } - - internal static MethodInfo ReacquisitionIfDeclaringTypeIsGenericTypeDefinition(this MethodInfo methodInfo, Type closedGenericType) - { - if (!methodInfo.DeclaringType.GetTypeInfo().IsGenericTypeDefinition) - { - return methodInfo; - } - - return closedGenericType.GetTypeInfo().GetMethod(methodInfo.Name, methodInfo.GetParameterTypes()); - } - - - - internal static string GetFullName(this MemberInfo member) - { - var declaringType = member.DeclaringType.GetTypeInfo(); - if (declaringType.IsInterface) - { - return $"{declaringType.Name}.{member.Name}".Replace('+', '.'); - } - return member.Name; - } - - internal static bool IsReturnTask(this MethodInfo methodInfo) - { - return typeof(Task).GetTypeInfo().IsAssignableFrom(methodInfo.ReturnType.GetTypeInfo()); - } - - } -}