Пример #1
0
        /// <summary>
        /// Uses reflection to dynamically invoke a method,
        /// throwing an exception if it is not
        /// implemented on the target object.
        /// </summary>
        /// <param name="obj">
        /// Object containing method.
        /// </param>
        /// <param name="info">
        /// MethodInfo for the method.
        /// </param>
        /// <param name="parameters">
        /// Parameters to pass to method.
        /// </param>
        public static object CallMethod(object obj, MethodInfo info, params object[] parameters)
        {
            DynamicMethodHandle cachedMethod = MethodCaller.GetCachedMethod(obj, info, parameters);

            if (cachedMethod == null || cachedMethod.DynamicMethod == null)
            {
                throw new NotImplementedException(info.Name + " not implemented.");
            }
            return(MethodCaller.CallMethod(obj, cachedMethod, parameters));
        }
Пример #2
0
        /// <summary>
        /// Uses reflection to create an object using its
        /// default constructor.
        /// </summary>
        /// <param name="objectType">Type of object to create.</param>
        public static object CreateInstance(Type objectType)
        {
            DynamicConstructor cachedConstructor = MethodCaller.GetCachedConstructor(objectType);

            if (cachedConstructor == null)
            {
                throw new NotImplementedException("Default Constructor not implemented.");
            }
            return(cachedConstructor());
        }
Пример #3
0
        /// <summary>
        /// Uses reflection to dynamically invoke a method
        /// if that method is implemented on the target object.
        /// </summary>
        /// <param name="obj">
        /// Object containing method.
        /// </param>
        /// <param name="method">
        /// Name of the method.
        /// </param>
        /// <param name="parameters">
        /// Parameters to pass to method.
        /// </param>
        public static object CallMethodIfImplemented(object obj, string method, params object[] parameters)
        {
            DynamicMethodHandle cachedMethod = MethodCaller.GetCachedMethod(obj, method, parameters);

            if (cachedMethod == null || cachedMethod.DynamicMethod == null)
            {
                return(null);
            }
            return(MethodCaller.CallMethod(obj, cachedMethod, parameters));
        }
Пример #4
0
 /// <summary>
 /// Gets an object's property value by name.
 /// </summary>
 /// <param name="target">Object containing the property to get.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <param name="useCache">if set to <c>true</c> use dynamic cache.</param>
 /// <returns>The value of the property.</returns>
 public static object GetPropertyValue(object target, string propertyName, bool useCache)
 {
     if (target == null)
     {
         throw new ArgumentNullException("target");
     }
     if (string.IsNullOrEmpty(propertyName))
     {
         throw new ArgumentNullException("propertyName");
     }
     if (useCache)
     {
         return(MethodCaller.GetCachedProperty(target.GetType(), propertyName).DynamicMemberGet(target));
     }
     return(MethodCaller.FindProperty(target.GetType(), propertyName).GetValue(target, null));
 }
Пример #5
0
 internal static string[] GetCachedPropertyNames(Type objectType)
 {
     string[] propertyNames;
     if (MethodCaller._propertyNameCache.TryGetValue(objectType, out propertyNames))
     {
         return(propertyNames);
     }
     lock (MethodCaller._propertyNameCache)
     {
         if (!MethodCaller._propertyNameCache.TryGetValue(objectType, out propertyNames))
         {
             propertyNames = MethodCaller.GetPropertyNames(objectType);
             MethodCaller._propertyNameCache.Add(objectType, propertyNames);
         }
     }
     return(propertyNames);
 }
Пример #6
0
 /// <summary>
 /// Copies values from the source into the target <see cref="T:System.Collections.IDictionary" />.
 /// </summary>
 /// <param name="source">The source object.</param>
 /// <param name="target">The target <see cref="T:System.Collections.IDictionary" />.</param>
 /// <param name="settings">The settings to use when copying properties.</param>
 public static void Copy(object source, IDictionary <string, object> target, ObjectCopierSettings settings)
 {
     string[] cachedPropertyNames;
     if (source == null)
     {
         throw new ArgumentNullException("source", "Source object can not be Null.");
     }
     if (target == null)
     {
         throw new ArgumentNullException("target", "Target object can not be Null.");
     }
     if (settings == null)
     {
         settings = new ObjectCopierSettings();
     }
     if (settings.UseDynamicCache)
     {
         cachedPropertyNames = MethodCaller.GetCachedPropertyNames(source.GetType());
     }
     else
     {
         cachedPropertyNames = MethodCaller.GetPropertyNames(source.GetType());
     }
     foreach (string str in cachedPropertyNames)
     {
         if (!settings.IgnoreList.Contains(str))
         {
             try
             {
                 object obj2 = GetPropertyValue(source, str, settings.UseDynamicCache);
                 target.Add(str, obj2);
             }
             catch (Exception exception)
             {
                 if (!settings.SuppressExceptions)
                 {
                     throw new ArgumentException(string.Format("Property '{0}' copy failed.", str), exception);
                 }
             }
         }
     }
 }
Пример #7
0
        private static DynamicMethodHandle GetCachedMethod(object obj, string method, params object[] parameters)
        {
            MethodCacheKey      key = new MethodCacheKey(obj.GetType().FullName, method, MethodCaller.GetParameterTypes(parameters));
            DynamicMethodHandle dynamicMethodHandle = null;

            if (MethodCaller._methodCache.TryGetValue(key, out dynamicMethodHandle))
            {
                return(dynamicMethodHandle);
            }
            lock (MethodCaller._methodCache)
            {
                if (!MethodCaller._methodCache.TryGetValue(key, out dynamicMethodHandle))
                {
                    MethodInfo method2 = MethodCaller.GetMethod(obj.GetType(), method, parameters);
                    dynamicMethodHandle = new DynamicMethodHandle(method2, parameters);
                    MethodCaller._methodCache.Add(key, dynamicMethodHandle);
                }
            }
            return(dynamicMethodHandle);
        }
Пример #8
0
        /// <summary>
        /// Uses reflection to dynamically invoke a method,
        /// throwing an exception if it is not implemented
        /// on the target object.
        /// </summary>
        /// <param name="obj">
        /// Object containing method.
        /// </param>
        /// <param name="methodHandle">
        /// MethodHandle for the method.
        /// </param>
        /// <param name="parameters">
        /// Parameters to pass to method.
        /// </param>
        private static object CallMethod(object obj, DynamicMethodHandle methodHandle, params object[] parameters)
        {
            object result = null;
            DynamicMemberMethod arg_08_0 = methodHandle.DynamicMethod;

            object[] array2;
            if (parameters == null)
            {
                object[] array = new object[1];
                array2 = array;
            }
            else
            {
                array2 = parameters;
            }
            if (methodHandle.HasFinalArrayParam)
            {
                int      methodParamsLength = methodHandle.MethodParamsLength;
                int      num         = array2.Length - (methodParamsLength - 1);
                object[] extrasArray = MethodCaller.GetExtrasArray(num, methodHandle.FinalArrayElementType);
                Array.Copy(array2, extrasArray, num);
                object[] array3 = new object[methodParamsLength];
                for (int i = 0; i <= methodParamsLength - 2; i++)
                {
                    array3[i] = parameters[i];
                }
                array3[array3.Length - 1] = extrasArray;
                array2 = array3;
            }
            try
            {
                result = methodHandle.DynamicMethod(obj, array2);
            }
            catch (Exception ex)
            {
                throw new CallMethodException(methodHandle.MethodName + " method call failed.", ex);
            }
            return(result);
        }
Пример #9
0
 /// <summary>
 /// Sets an object's property with the specified value,
 /// converting that value to the appropriate type if possible.
 /// </summary>
 /// <param name="target">Object containing the property to set.</param>
 /// <param name="propertyName">Name of the property to set.</param>
 /// <param name="value">Value to set into the property.</param>
 /// <param name="useCache">if set to <c>true</c> use dynamic cache.</param>
 public static void SetPropertyValue(object target, string propertyName, object value, bool useCache)
 {
     if (target == null)
     {
         throw new ArgumentNullException("target", "Target object can not be Null.");
     }
     if (useCache)
     {
         DynamicMemberHandle cachedProperty = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
         if (cachedProperty != null)
         {
             SetValueWithCoercion(target, cachedProperty, value);
         }
     }
     else
     {
         PropertyInfo handle = MethodCaller.FindProperty(target.GetType(), propertyName);
         if (handle != null)
         {
             SetValueWithCoercion(target, handle, value);
         }
     }
 }
Пример #10
0
        internal static DynamicMemberHandle GetCachedProperty(Type objectType, string propertyName)
        {
            MethodCacheKey      key = new MethodCacheKey(objectType.FullName, propertyName, MethodCaller.GetParameterTypes(null));
            DynamicMemberHandle dynamicMemberHandle = null;

            if (MethodCaller._memberCache.TryGetValue(key, out dynamicMemberHandle))
            {
                return(dynamicMemberHandle);
            }
            lock (MethodCaller._memberCache)
            {
                if (!MethodCaller._memberCache.TryGetValue(key, out dynamicMemberHandle))
                {
                    PropertyInfo propertyInfo = MethodCaller.FindProperty(objectType, propertyName);
                    if (propertyInfo != null)
                    {
                        dynamicMemberHandle = new DynamicMemberHandle(propertyInfo);
                    }
                    MethodCaller._memberCache.Add(key, dynamicMemberHandle);
                }
            }
            return(dynamicMemberHandle);
        }
Пример #11
0
 /// <summary>
 /// Finds a <see cref="T:System.Reflection.PropertyInfo" /> by name ignoring case.
 /// </summary>
 /// <param name="type">The type to search.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <returns>A <see cref="T:System.Reflection.PropertyInfo" /> matching the property name.</returns>
 /// <remarks>
 /// FindProperty will first try to get a property matching the name and case of the
 /// property name specified.  If a property cannot be found, all the properties will
 /// be searched ignoring the case of the name.
 /// </remarks>
 public static PropertyInfo FindProperty(Type type, string propertyName)
 {
     return(MethodCaller.FindProperty(type, propertyName));
 }
Пример #12
0
        /// <summary>
        /// Invokes a property setter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <param name="value">New value for property.</param>
        public static void CallPropertySetter(object obj, string property, object value)
        {
            DynamicMemberHandle cachedProperty = MethodCaller.GetCachedProperty(obj.GetType(), property);

            cachedProperty.DynamicMemberSet(obj, value);
        }
Пример #13
0
        /// <summary>
        /// Invokes a property getter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <returns></returns>
        public static object CallPropertyGetter(object obj, string property)
        {
            DynamicMemberHandle cachedProperty = MethodCaller.GetCachedProperty(obj.GetType(), property);

            return(cachedProperty.DynamicMemberGet(obj));
        }
Пример #14
0
        internal static DynamicMemberHandle GetCachedField(Type objectType, string fieldName)
        {
            MethodCacheKey      key = new MethodCacheKey(objectType.FullName, fieldName, MethodCaller.GetParameterTypes(null));
            DynamicMemberHandle dynamicMemberHandle = null;

            if (MethodCaller._memberCache.TryGetValue(key, out dynamicMemberHandle))
            {
                return(dynamicMemberHandle);
            }
            lock (MethodCaller._memberCache)
            {
                if (!MethodCaller._memberCache.TryGetValue(key, out dynamicMemberHandle))
                {
                    FieldInfo field = objectType.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (field != null)
                    {
                        dynamicMemberHandle = new DynamicMemberHandle(field);
                    }
                    MethodCaller._memberCache.Add(key, dynamicMemberHandle);
                }
            }
            return(dynamicMemberHandle);
        }