Ejemplo n.º 1
0
        /// <summary>
        /// Returns true if the specified instance is of a type
        /// that has the specified propertyName
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static bool HasProperty(this object instance, string propertyName)
        {
            Args.ThrowIfNull(instance, "instance");
            PropertyInfo ignore;

            return(HasProperty(instance, propertyName, out ignore));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Unsubscribe the specified handler from the specified event
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <param name="eventName"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static object UnSubscribe(this object instance, string eventName, Delegate handler)
        {
            EventInfo eventInfo = instance.GetType().GetEvent(eventName);

            Args.ThrowIfNull(eventInfo, "eventName");
            eventInfo.RemoveEventHandler(instance, handler);
            return(instance);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Unsubscribe the specified handler from the specified event
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <param name="eventName"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static T UnSubscribe <T>(this T instance, string eventName, Delegate handler)
        {
            EventInfo eventInfo = typeof(T).GetEvent(eventName);

            Args.ThrowIfNull(eventInfo, "eventName");
            eventInfo.RemoveEventHandler(instance, handler);
            return(instance);
        }
Ejemplo n.º 4
0
        public static MemoryStream ToBinaryStream(this object target)
        {
            Args.ThrowIfNull(target, "target");
            MemoryStream stream = new MemoryStream();

            ToBinaryStream(target, stream);
            return(stream);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns true if the specified instance is of a type
        /// that has the specified propertyName
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="propertyName"></param>
        /// <param name="prop"></param>
        /// <returns></returns>
        public static bool HasProperty(this object instance, string propertyName, out PropertyInfo prop)
        {
            Args.ThrowIfNull(instance, "instance");
            Type type = instance.GetType();

            prop = type.GetProperty(propertyName);
            return(prop != null);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Set the property with the specified name and return the instance
        /// to enable chaining
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="throwIfPropertyNotFound"></param>
        /// <returns></returns>
        public static object Property <T>(this T instance, string propertyName, object value, bool throwIfPropertyNotFound = true)
        {
            Args.ThrowIfNull(instance, "instance");
            Type         type     = instance.GetType(); // get the actual instance type since it may be an extender of T
            PropertyInfo property = GetPropertyOrThrow(type, propertyName, throwIfPropertyNotFound);

            SetProperty(instance, property, value);
            return(instance);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Set the property with the specified name and return the instance
        /// to enable chaining
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="instanceType"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="throwIfPropertyNotFound"></param>
        /// <returns></returns>
        public static object Property(this object instance, Type instanceType, string propertyName, object value, bool throwIfPropertyNotFound = true)
        {
            Args.ThrowIfNull(instance, "instance");
            Args.ThrowIfNull(instanceType, "instanceType");
            PropertyInfo property = GetPropertyOrThrow(instanceType, propertyName, throwIfPropertyNotFound);

            SetProperty(instance, property, value);
            return(instance);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Replaces instances of named properties in the specified format with the property values of the specified dataSource.
        /// Named properties in the specified format string should be in the form "{PropertyName}".  This uses a basic string
        /// replacement mechanism, for more robust templating use a template engine.
        /// </summary>
        /// <param name="format"></param>
        /// <param name="dataSource"></param>
        /// <param name="opener"></param>
        /// <param name="closer"></param>
        /// <returns></returns>
        public static string NamedFormat(this string format, Dictionary <string, string> dataSource, string opener = "{", string closer = "}")
        {
            Args.ThrowIfNull(dataSource, nameof(dataSource));
            string returnValue = format;

            foreach (string key in dataSource.Keys)
            {
                returnValue = returnValue.Replace($"{opener}{key}{closer}", dataSource[key]);
            }

            return(returnValue);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Replaces instances of named properties in the specified format with the property values of the specified dataSource.
        /// Named properties in the specified format string should be in the form "{PropertyName}".  This uses a basic string
        /// replacement mechanism, for more robust templating use a template engine.
        /// </summary>
        /// <param name="format"></param>
        /// <param name="dataSource"></param>
        /// <param name="opener"></param>
        /// <param name="closer"></param>
        /// <returns></returns>
        public static string NamedFormat(this string format, object dataSource, string opener = "{", string closer = "}")
        {
            Args.ThrowIfNull(dataSource, nameof(dataSource));
            PropertyInfo[] props       = dataSource.GetType().GetProperties();
            string         returnValue = format;

            foreach (PropertyInfo prop in props)
            {
                returnValue = returnValue.Replace($"{opener}{prop.Name}{closer}", prop.GetValue(dataSource)?.ToString());
            }
            return(returnValue);
        }
Ejemplo n.º 10
0
        public static object ToDynamicData(this object instance, string typeName)
        {
            Args.ThrowIfNull(instance);
            Type    type    = instance.GetType();
            JObject jObject = new JObject();

            foreach (PropertyInfo prop in type.GetProperties().Where(PropertyDataTypeFilter))
            {
                jObject.Add(prop.Name, new JObject(prop.GetValue(instance)));
            }
            return(jObject);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Return the results of the eacher by passing the PropertyInfo and value
 /// of each of the properties of instance where the property is declared
 /// on the specified type and the property type is one of the base
 /// supported data types; bool, int, long, decimal, string, byte[],
 /// DateTime or their nullable equivalent
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="instance"></param>
 /// <param name="type"></param>
 /// <param name="propertyPredicate"></param>
 /// <param name="eacher"></param>
 /// <returns></returns>
 public static IEnumerable <T> EachDataProperty <T>(this object instance, Type type, Func <PropertyInfo, bool> propertyPredicate, Func <PropertyInfo, object, T> eacher)
 {
     Args.ThrowIfNull(propertyPredicate, nameof(propertyPredicate));
     foreach (PropertyInfo pi in type.GetProperties().Where(pi => pi.DeclaringType == type && pi.PropertyType.In(typeof(bool?), typeof(bool), typeof(int), typeof(int?), typeof(long), typeof(long?), typeof(decimal), typeof(decimal?), typeof(string), typeof(byte[]), typeof(DateTime), typeof(DateTime?))))
     {
         if (propertyPredicate(pi))
         {
             yield return(eacher(pi, pi.GetValue(instance)));
         }
     }
     ;
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Tries to get the value of the specified property, using the specified value if
        /// the property is not found on the specified instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="ifPropertyNotFound">If property not found.</param>
        /// <param name="value">The value.</param>
        public static void TryGetPropertyValue <T>(this object instance, string propertyName, T ifPropertyNotFound, out T value)
        {
            Args.ThrowIfNull(instance, "instance");
            Type         type     = instance.GetType();
            PropertyInfo property = type.GetProperties().FirstOrDefault(pi => pi.Name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase));

            if (property == null)
            {
                value = ifPropertyNotFound;
            }
            else
            {
                value = (T)property.GetValue(instance);
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Invoke the specified generic method with generic argument type TArg
 /// returning object of type T
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="TArg"></typeparam>
 /// <param name="instance"></param>
 /// <param name="methodName"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public static T InvokeGeneric <T, TArg>(this object instance, string methodName, params object[] args)
 {
     Args.ThrowIfNull(instance, "instance");
     Args.ThrowIfNull(methodName, "methodName");
     try
     {
         MethodInfo method        = instance.GetType().GetMethod(methodName, args.Select(a => a.GetType()).ToArray());
         MethodInfo genericMethod = method.MakeGenericMethod(typeof(TArg));
         return((T)genericMethod.Invoke(instance, args));
     }
     catch (AmbiguousMatchException ame)
     {
         IEnumerable <MethodInfo> methods = instance.GetType().GetMethods().Where(mi => mi.Name.Equals(methodName) && mi.ContainsGenericParameters && mi.GetParameters().Length == args.Length);
         T    response = default(T);
         bool gotOne   = false;
         foreach (MethodInfo method in methods)
         {
             ParameterInfo[] paramInfos = method.GetParameters();
             bool            useThisOne = true;
             for (int i = 0; i < paramInfos.Length; i++)
             {
                 Type argType   = args[i].GetType();
                 Type paramType = paramInfos[i].ParameterType;
                 if (paramType == typeof(object))
                 {
                     continue;
                 }
                 if (!argType.Equals(paramType))
                 {
                     useThisOne = false;
                     break;
                 }
             }
             if (useThisOne)
             {
                 MethodInfo genericMethod = method.MakeGenericMethod(typeof(TArg));
                 response = (T)genericMethod.Invoke(instance, args);
                 gotOne   = true;
                 break;
             }
         }
         if (!gotOne)
         {
             throw ame;
         }
         return(response);
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Get the property of the current instance with the specified name
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object Property(this object instance, string propertyName, bool throwIfPropertyNotFound = true)
        {
            Args.ThrowIfNull(instance, "instance");
            Type         type     = instance.GetType();
            PropertyInfo property = type.GetProperties().FirstOrDefault(pi => pi.Name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase));

            if (property == null)
            {
                if (throwIfPropertyNotFound)
                {
                    PropertyNotFound(propertyName, type);
                }
                else
                {
                    return(null);
                }
            }
            return(property.GetValue(instance));
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Invoke the specified method on the specified instance
 /// using the specified arguments
 /// </summary>
 /// <param name="instance"></param>
 /// <param name="methodName"></param>
 /// <param name="args"></param>
 public static object Invoke(this object instance, string methodName, params object[] args)
 {
     Args.ThrowIfNull(instance, "instance");
     Args.ThrowIfNull(methodName, "methodName");
     return(instance.GetType().GetMethod(methodName).Invoke(instance, args));
 }
 public void Render(object toRender, Stream output)
 {
     Args.ThrowIfNull(toRender, "toRender");
     Render(toRender.GetType().Name, toRender, output);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Invoke the specified method on the specified instance
 /// using the specified arguments
 /// </summary>
 /// <param name="instance"></param>
 /// <param name="methodName"></param>
 /// <param name="args"></param>
 public static T Invoke <T>(this object instance, string methodName, params object[] args)
 {
     Args.ThrowIfNull(instance, "instance");
     Args.ThrowIfNull(methodName, "methodName");
     return((T)instance.GetType().GetMethod(methodName, args.Select(a => a.GetType()).ToArray()).Invoke(instance, args));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Invoke the specified static method of the
 /// specified (extension method "current") type
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="type"></param>
 /// <param name="methodName"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public static T InvokeStatic <T>(this Type type, string methodName, params object[] args)
 {
     Args.ThrowIfNull(type);
     Args.ThrowIfNull(methodName);
     return((T)type.GetMethod(methodName).Invoke(null, args));
 }
Ejemplo n.º 19
0
 public static object ToDynamicData(this object instance)
 {
     Args.ThrowIfNull(instance, "instance");
     return(ToDynamicData(instance, instance.GetType().Name));
 }