public bool makeTheRequestQueryStringDataEditable() { try { object objectToReflect = HttpContext.Current.Request.QueryString; string stringObjectType = "MakeReadWrite"; // Assert the retrival of information from a Private Method ReflectionPermission reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation); reflectionPerm.Assert(); MethodInfo objTempMethodType = objectToReflect.GetType().GetMethod( stringObjectType,BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null,CallingConventions.Any,new Type[0] {},null); // Revert the previous assert since it is only possible to have one active Assert ReflectionPermission.RevertAssert(); // Assert the execution of a Private Method reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); reflectionPerm.Assert(); object invokeResult = objTempMethodType.Invoke( objectToReflect,BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,null,new object[0] {} ,null); return true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); return false; } }
static XPathDocumentWriter() { ReflectionPermission perm = new ReflectionPermission(PermissionState.Unrestricted); perm.Flags = ReflectionPermissionFlag.MemberAccess; try { perm.Assert(); Type t = typeof(XPathDocument); defaultConstructor = t.GetConstructor( BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, new ParameterModifier[0]); Debug.Assert(defaultConstructor != null, ".NET Framework implementation changed"); loadWriterMethod = t.GetMethod("LoadFromWriter", BindingFlags.Instance | BindingFlags.NonPublic); Debug.Assert(loadWriterMethod != null, ".NET Framework implementation changed"); } finally { CodeAccessPermission.RevertAssert(); } }
internal void SetAttributes(IDataReader reader) { // allow access to private fields ReflectionPermission perm = new ReflectionPermission(ReflectionPermissionFlag.NoFlags); perm.Demand(); perm.Assert(); try { IDictionary TimeSheetFieldMappings = GetDataLink().FieldMappings; _log.Debug("SetAttributes: starting for loop (" + this.GetType().FullName + ")"); for (int i = 0; i < reader.FieldCount; i++) { try { // Get the field mapping entry corresponding to the database column name TimeSheetFieldMap TimeSheetFieldMap = null; foreach (TimeSheetFieldMap TimeSheetFieldMap2 in TimeSheetFieldMappings.Values) { if (TimeSheetFieldMap2.FieldName == reader.GetName(i)) { TimeSheetFieldMap = TimeSheetFieldMap2; break; } } if (TimeSheetFieldMap == null) { //throw new ApplicationException(string.Format("Could not locate {0} in field mappings.", reader.GetName(i))); } // Read the object from the database. This may be a plain value // Enum stored as an integer in the database object value; if (!reader.IsDBNull(i)) { if (TimeSheetFieldMap.IsEnum) { // the database field holds the int value for an enum _log.Debug("SetAttributes: Setting enum value for " + TimeSheetFieldMap.AttributeName); object enumValue = GetObjectFromDatabase(reader, i); value = Enum.ToObject(TimeSheetFieldMap.EnumType, enumValue); } else { // the database field holds the attribute value _log.Debug("SetAttributes: Setting direct value for " + TimeSheetFieldMap.AttributeName); value = GetObjectFromDatabase(reader, i); } } else { value = null; } _log.Debug("SetAttributes: now setting field " + TimeSheetFieldMap.FieldInfo + " to " + value); // set the instance's field to the value try { TimeSheetFieldMap.FieldInfo.SetValue(this, value); } catch { _log.Debug("Exception caught in FieldInfo.SetValue setting field {0} to {1}.", TimeSheetFieldMap.FieldInfo, value); //throw; } _log.Debug("SetAttributes: Setting " + TimeSheetFieldMap.FieldName + " to " + value); } catch (Exception e) { _log.Debug("Exception caught reading {0}, column {1}, column name '{2}'.", e, this.GetType().Name, i, reader.GetName(i)); //throw; } } } finally { CodeAccessPermission.RevertAssert(); } _log.Debug("SetAttributes: done (" + this.GetType().FullName + ")"); }
public static object InvokePropertyOrMethod(string className, string methodName, object[] parameters, InvokeType invokeType) { System.Security.Permissions.ReflectionPermission perm = new System.Security.Permissions.ReflectionPermission( System.Security.Permissions.PermissionState.Unrestricted); perm.Assert(); Type type; bool isGetProperty; object[] newargs; object instance; BindingFlags defaultFlags; object resultObject; //if (ActionManager.IsInvokeTypeStatic(invokeType)) //{ // if (String.IsNullOrEmpty(className)) // { // throw new ArgumentException( // "className cannot be null or empty for static invocations", // "className"); // } // type = ReflectionUtils.FindType(className); // if (type == null) // { // throw new InvalidOperationException("FindType fails"); // } //} //else //{ if (parameters == null || parameters.Length == 0) { throw new ArgumentException( "parameters cannot be null or empty for instance invocations", "parameters"); } if (parameters[0] == null) { throw new ArgumentException( "parameters cannot have a null first element for instance invocations", "parameters"); } type = parameters[0].GetType(); //} System.Diagnostics.Debug.Assert(type != null); defaultFlags = BindingFlags.ExactBinding | BindingFlags.Public; newargs = null; instance = null; switch (invokeType) { case InvokeType.StaticMethod: defaultFlags |= BindingFlags.InvokeMethod; defaultFlags |= BindingFlags.Static; break; case InvokeType.InstanceMethod: defaultFlags |= BindingFlags.InvokeMethod; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.GetStaticProperty: defaultFlags |= BindingFlags.GetProperty; defaultFlags |= BindingFlags.Static; break; case InvokeType.SetStaticProperty: defaultFlags |= BindingFlags.SetProperty; defaultFlags |= BindingFlags.Static; break; case InvokeType.GetInstanceProperty: defaultFlags |= BindingFlags.GetProperty; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.SetInstanceProperty: defaultFlags |= BindingFlags.SetProperty; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.GetStaticField: defaultFlags |= BindingFlags.GetField; defaultFlags |= BindingFlags.Static; break; case InvokeType.SetStaticField: defaultFlags |= BindingFlags.SetField; defaultFlags |= BindingFlags.Static; break; case InvokeType.GetInstanceField: defaultFlags |= BindingFlags.GetField; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.SetInstanceField: defaultFlags |= BindingFlags.SetField; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; } try { resultObject = type.InvokeMember( methodName, defaultFlags, null, instance, parameters); } catch (MissingMethodException e) { isGetProperty = invokeType == InvokeType.GetInstanceProperty || invokeType == InvokeType.GetStaticProperty; if (parameters.Length > 0 && isGetProperty) { } throw; } catch (AmbiguousMatchException e) { throw; } return resultObject; }
public static object InvokePropertyOrMethod(string className, string methodName, object[] parameters, InvokeType invokeType) { System.Security.Permissions.ReflectionPermission perm = new System.Security.Permissions.ReflectionPermission( System.Security.Permissions.PermissionState.Unrestricted); perm.Assert(); Type type; bool isGetProperty; object[] newargs; object instance; BindingFlags defaultFlags; object resultObject; //if (ActionManager.IsInvokeTypeStatic(invokeType)) //{ // if (String.IsNullOrEmpty(className)) // { // throw new ArgumentException( // "className cannot be null or empty for static invocations", // "className"); // } // type = ReflectionUtils.FindType(className); // if (type == null) // { // throw new InvalidOperationException("FindType fails"); // } //} //else //{ if (parameters == null || parameters.Length == 0) { throw new ArgumentException( "parameters cannot be null or empty for instance invocations", "parameters"); } if (parameters[0] == null) { throw new ArgumentException( "parameters cannot have a null first element for instance invocations", "parameters"); } type = parameters[0].GetType(); //} System.Diagnostics.Debug.Assert(type != null); defaultFlags = BindingFlags.ExactBinding | BindingFlags.Public; newargs = null; instance = null; switch (invokeType) { case InvokeType.StaticMethod: defaultFlags |= BindingFlags.InvokeMethod; defaultFlags |= BindingFlags.Static; break; case InvokeType.InstanceMethod: defaultFlags |= BindingFlags.InvokeMethod; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.GetStaticProperty: defaultFlags |= BindingFlags.GetProperty; defaultFlags |= BindingFlags.Static; break; case InvokeType.SetStaticProperty: defaultFlags |= BindingFlags.SetProperty; defaultFlags |= BindingFlags.Static; break; case InvokeType.GetInstanceProperty: defaultFlags |= BindingFlags.GetProperty; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.SetInstanceProperty: defaultFlags |= BindingFlags.SetProperty; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.GetStaticField: defaultFlags |= BindingFlags.GetField; defaultFlags |= BindingFlags.Static; break; case InvokeType.SetStaticField: defaultFlags |= BindingFlags.SetField; defaultFlags |= BindingFlags.Static; break; case InvokeType.GetInstanceField: defaultFlags |= BindingFlags.GetField; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; case InvokeType.SetInstanceField: defaultFlags |= BindingFlags.SetField; defaultFlags |= BindingFlags.Instance; instance = ExtractFirstParameterAsInstance(parameters, ref newargs); parameters = newargs; break; } try { resultObject = type.InvokeMember( methodName, defaultFlags, null, instance, parameters); } catch (MissingMethodException e) { isGetProperty = invokeType == InvokeType.GetInstanceProperty || invokeType == InvokeType.GetStaticProperty; if (parameters.Length > 0 && isGetProperty) { } throw; } catch (AmbiguousMatchException e) { throw; } return(resultObject); }