/// <summary> /// Creates a new instance of the safe field wrapper. /// </summary> /// <param name="field">Field to wrap.</param> public SafeField(FieldInfo field) { AssertUtils.ArgumentNotNull(field, "You cannot create a dynamic field for a null value."); this.fieldInfo = field; this.canSet = (!fieldInfo.IsLiteral && !fieldInfo.IsInitOnly && !(fieldInfo.DeclaringType.IsValueType && !fieldInfo.IsStatic)); if (fieldInfo.IsPublic && ReflectionUtils.IsTypeVisible(fieldInfo.DeclaringType, DynamicReflectionManager.ASSEMBLY_NAME)) { dynamicField = DynamicField.Create(fieldInfo); isOptimizedGet = isOptimizedSet = true; } }
/// <summary> /// Returns a <see cref="IDynamicField"/> implementation /// by determining the fastest possible dynamic access strategy /// </summary> /// <param name="field">the field to be wrapped</param> /// <returns>an <see cref="IDynamicField"/> instance for accessing the /// field represented by the given <see cref="FieldInfo"/></returns> public static IDynamicField CreateFrom(FieldInfo field) { AssertUtils.ArgumentNotNull(field, "You cannot create a dynamic field for a null value."); IDynamicField dynamicField; if (field.IsPublic && ReflectionUtils.IsTypeVisible(field.DeclaringType, DynamicReflectionManager.ASSEMBLY_NAME)) { dynamicField = DynamicField.Create(field); } else { dynamicField = new SafeField(field); } return(dynamicField); }
protected static object GetVisualBasicTestObject() { if (s__visualBasicTestObjectType == null) { // compile vb test class string vbSourceCode = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(BasePropertyTests), "SafePropertyTests_TestObject.vb")).ReadToEnd(); CompilerParameters args = new CompilerParameters(); args.OutputAssembly = "VbTestObject.dll"; args.GenerateInMemory = true; args.GenerateExecutable = false; args.IncludeDebugInformation = true; #if NET_2_0 CodeDomProvider provider = CodeDomProvider.CreateProvider("VisualBasic"); CompilerResults results = provider.CompileAssemblyFromSource(args, vbSourceCode); #else CodeDomProvider provider = new VBCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerResults results = compiler.CompileAssemblyFromSource(args, vbSourceCode); #endif if (results.Errors.HasErrors) { StringBuilder sb = new StringBuilder(); foreach (CompilerError error in results.Errors) { sb.Append(error.ToString()).Append("\n\r"); } throw new TypeLoadException("failed compiling test class: " + sb); } s__visualBasicTestObjectType = results.CompiledAssembly.GetType("VbTestObject"); ThisLastPropertyValue = DynamicField.Create(s__visualBasicTestObjectType.GetField("ThisLastPropertyValue")); ThisArg1 = DynamicField.Create(s__visualBasicTestObjectType.GetField("ThisArg1")); ThisArg2 = DynamicField.Create(s__visualBasicTestObjectType.GetField("ThisArg2")); ThisOptionalArg = DynamicField.Create(s__visualBasicTestObjectType.GetField("ThisOptionalArg")); ThisParamsArg = DynamicField.Create(s__visualBasicTestObjectType.GetField("ThisParamsArgs")); } object s__visualBasicTestObject = Activator.CreateInstance(s__visualBasicTestObjectType); return(s__visualBasicTestObject); }
protected virtual IDynamicField Create(FieldInfo field) { return(DynamicField.Create(field)); }