public static bool IsImplicitlyCastableTo(this Type from, Type to) { AssertTools.Assert(from != null); AssertTools.Assert(to != null); // not strictly necessary, but speeds things up if (to.IsAssignableFrom(from)) { return(true); } try { // overload of GetMethod() from http://www.codeducky.org/10-utilities-c-developers-should-know-part-two/ // that takes Expression<Action> GetMethod(() => AttemptImplicitCast <object, object>()) .GetGenericMethodDefinition() .MakeGenericMethod(from, to) .Invoke(null, new object[0]); return(true); } catch (TargetInvocationException ex) { return(!( ex.InnerException is RuntimeBinderException // if the code runs in an environment where this message is localized, we could attempt a known failure first and base the regex on it's message && Regex.IsMatch(ex.InnerException.Message, @"^The best overloaded method match for 'System.Collections.Generic.List<.*>.Add(.*)' has some invalid arguments$") )); } }
public static T GetStaticProperyValue <T>(this Type type, string propertyName) { AssertTools.Assert(type != null, "Type cannot be null."); var property = GetPropertyInfo(type, propertyName, true, false); AssertTools.Assert(property != null, String.Format("Static property {0} not found on type {1}.", propertyName, type.FullName)); return((T)property.GetValue(null, null)); }
public static void SetProperyValue <T>(this object obj, string propertyName, T value) { AssertTools.Assert(obj != null, "Object cannot be null."); var type = obj.GetType(); var property = GetPropertyInfo(type, propertyName); AssertTools.Assert(property != null, String.Format("Property {0} not found on type {1}.", propertyName, type.FullName)); property.SetValue(obj, value, null); }
public static void SetFieldValue <T>(this object obj, string fieldName, T value) { AssertTools.Assert(obj != null, "Object cannot be null."); var type = obj.GetType(); var field = GetFieldInfo(type, fieldName); AssertTools.Assert(field != null, String.Format("Field {0} not found on type {1}.", fieldName, type.FullName)); field.SetValue(obj, value); }
public static T StaticBaseMethodExecute <T>(Type type, string methodName, object[] parameters, Type[] types = null) { var basetype = type.BaseType; AssertTools.Assert(basetype != null, String.Format("Type {0} has no basetype.", type.FullName)); var method = GetStaticMethodInfo(basetype, methodName, types); AssertTools.Assert(method != null, String.Format("Static method {0} not found on type {1}.", methodName, type.FullName)); return((T)method.Invoke(null, parameters)); }
public static T BaseMethodExecute <T>(this object obj, string methodName, object[] parameters = null, Type[] types = null) { AssertTools.Assert(obj != null, "Object cannot be null."); var baseType = obj.GetType().BaseType; AssertTools.Assert(baseType != null, String.Format("Type {0} has no basetype.", obj.GetType().FullName)); var method = GetMethodInfo(baseType, methodName, types); AssertTools.Assert(method != null, String.Format("Method {0} not found on type {1}.", methodName, baseType.FullName)); return((T)method.Invoke(obj, parameters)); }
public static void SetFieldOrPropertyValue <T>(this object obj, string fieldOrPropertyName, T value) { AssertTools.Assert(obj != null, "Object cannot be null."); var type = obj.GetType(); var field = GetFieldInfo(type, fieldOrPropertyName); if (field != null) { field.SetValue(obj, value); return; } var property = GetPropertyInfo(type, fieldOrPropertyName); if (property != null) { property.SetValue(obj, value, null); return; } throw new Exception(string.Format("Field or property {0} not found on type {1}.", fieldOrPropertyName, type.FullName)); }
public static void AssertNotDotDot(string fileName) { AssertTools.Assert(!fileName.Contains(".."), string.Format("File name cannot include 2 dots. File name={0}", fileName)); }