private FieldReference CreateTargetField(ClassEmitter emitter) { var targetField = emitter.CreateField("__target", targetType); emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField); return(targetField); }
private void CreateTargetField(ClassEmitter emitter) { targetField = emitter.CreateField("__target", targetType); #if FEATURE_SERIALIZATION emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField); #endif }
private void CreateFields(ClassEmitter emitter, Type proxyTargetType) { base.CreateFields(emitter); targetField = emitter.CreateField("__target", proxyTargetType); emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField); }
public void Generate(ClassEmitter @class, ProxyGenerationOptions options, INamingScope namingScope) { var baseClass = @class.BaseType; foreach (var partType in parts) { var specificInterface = genericInterface.MakeGenericType(partType); var typeBuilder = @class.TypeBuilder; typeBuilder.AddInterfaceImplementation(specificInterface); var field = @class.CreateField(string.Format("_part<{0}>", partType.Name), partType);; var partProperty = specificInterface.GetMethod("get_Part", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); constructorContributor.AppendConstructorStatement( new AssignStatement(field, new NewInstanceExpression(partType, new Type[0]))); var getMethodBuilder = typeBuilder.DefineMethod( string.Format("get_Part<{0}>", partType.Name), propertyAttributes, partType, null); var getILGenerator = getMethodBuilder.GetILGenerator(); getILGenerator.Emit(OpCodes.Ldarg_0); getILGenerator.Emit(OpCodes.Ldfld, field.Reference); getILGenerator.Emit(OpCodes.Ret); typeBuilder.DefineMethodOverride(getMethodBuilder, partProperty); } }
/// <summary> /// Builds the adapter. /// </summary> /// <returns></returns> public Type Build() { //Setup emitter ClassEmitter classEmitter = new ClassEmitter(ModuleScope, OriginalObject.Name + "Adapter", typeof(AdapterBase), new Type[] { }, TypeAttributes.Class, true); //Add a field to hold a reference to the original object that is being adapter. FieldReference adaptedObjectReference = classEmitter.CreateField("_Original", OriginalObject); //Add a constructor that accepts a reference to the original object and //assigns that reference to the field. ArgumentReference parameter = new ArgumentReference(OriginalObject); ConstructorEmitter constructor = classEmitter.CreateConstructor(parameter); constructor.CodeBuilder.AddStatement( new AssignStatement(adaptedObjectReference, new ReferenceExpression(parameter))); constructor.CodeBuilder.AddStatement(new ReturnStatement()); //For each method, walk the pipeline foreach (MethodInfo method in OriginalObject.GetMethods()) { AdapterBuilderStageContext context = new AdapterBuilderStageContext(OriginalObject, classEmitter, adaptedObjectReference, method); WalkPipeline(context); } //build the type return(classEmitter.BuildType()); }
public void TryFinallyWithException() { FieldReference tryField = ClassEmitter.CreateField("TryExecuted", typeof(bool)); FieldReference finallyField = ClassEmitter.CreateField("FinallyExecuted", typeof(bool)); var methodEmitter = GetMethodEmitter(false, typeof(void), new Type[0]); Statement[] tryBlock = new Statement[] { new ThrowStatement(typeof(Exception), "Expected exception"), new AssignStatement(tryField, new ConstReference(true).ToExpression()) }; Statement[] finallyBlock = new Statement[] { new AssignStatement(finallyField, new ConstReference(true).ToExpression()) }; methodEmitter.AddStatement(new TryFinallyStatement(tryBlock, finallyBlock)); methodEmitter.AddStatement(new ReturnStatement()); try { InvokeMethod(); Assert.Fail("Expected exception"); } catch (Exception ex) { Assert.That(ex.GetType(), Is.EqualTo(typeof(Exception))); Assert.That(ex.Message, Is.EqualTo("Expected exception")); } Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), tryField.Reference.Name), Is.False); Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), finallyField.Reference.Name), Is.True); }
protected void CreateInterceptorsField(ClassEmitter emitter) { var interceptorsField = emitter.CreateField("__interceptors", typeof(IInterceptor[])); #if !SILVERLIGHT emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(interceptorsField); #endif }
protected void CreateInterceptorsField(ClassEmitter emitter) { var interceptorsField = emitter.CreateField("__interceptors", typeof(IInterceptor[])); #if FEATURE_SERIALIZATION emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(interceptorsField); #endif }
private void CreateFields(ClassEmitter emitter, Type proxyTargetType) { base.CreateFields(emitter); targetField = emitter.CreateField("__target", proxyTargetType); #if FEATURE_SERIALIZATION emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField); #endif }
private FieldReference CreateTargetField(ClassEmitter emitter) { var targetField = emitter.CreateField("__target", targetType); //#if FEATURE_SERIALIZATION emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField); //#endif return(targetField); }
protected void CreateSelectorField(ClassEmitter emitter) { if (ProxyGenerationOptions.Selector == null) { return; } emitter.CreateField("__selector", typeof(IInterceptorSelector)); }
protected FieldReference BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, INamingScope namingScope) { var methodInterceptors = @class.CreateField( namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)), typeof(IInterceptor[]), false); @class.DefineCustomAttributeFor <XmlIgnoreAttribute>(methodInterceptors); return(methodInterceptors); }
public void CreateFieldWithAttributes() { ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(object), Type.EmptyTypes); emitter.CreateField("myField", typeof(string), FieldAttributes.FamANDAssem | FieldAttributes.InitOnly); Type t = emitter.BuildType(); FieldInfo field = t.GetField("myField", BindingFlags.NonPublic | BindingFlags.Instance); Assert.IsNotNull(field); Assert.AreEqual(FieldAttributes.FamANDAssem | FieldAttributes.InitOnly, field.Attributes); }
private void CreateFields(ClassEmitter emitter, Type proxyTargetType) { base.CreateFields(emitter); targetField = emitter.CreateField("__target", proxyTargetType); #if SILVERLIGHT #warning XmlIncludeAttribute is in silverlight, do we want to explore this? #else emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField); #endif }
public void TypedMethodInvocationWithComplexOwner() { FieldReference fieldReference = ClassEmitter.CreateField("CallTarget", typeof(ReferenceType)); FieldInfoReference fieldInfoReference = new FieldInfoReference(SelfReference.Self, fieldReference.Reference); var method = GetMethodEmitter(false, typeof(string), new Type[0]); method.AddStatement(new AssignStatement(fieldReference, new NewInstanceExpression(typeof(ReferenceType), Type.EmptyTypes))); method.ImplementByReturning(new TypedMethodInvocationExpression(fieldInfoReference, typeof(ReferenceType).GetMethod("Method"))); Assert.That(InvokeMethod(), Is.EqualTo("ReferenceTypeMethod")); }
public void TypedMethodInvocationWithArguments() { FieldReference fieldReference = ClassEmitter.CreateField("CallTarget", typeof(ReferenceType)); FieldInfoReference fieldInfoReference = new FieldInfoReference(SelfReference.Self, fieldReference.Reference); var method = GetMethodEmitter(false, typeof(Tuple <int, string>), new Type[0]); method.AddStatement(new AssignStatement(fieldReference, new NewInstanceExpression(typeof(ReferenceType), Type.EmptyTypes))); method.ImplementByReturning(new TypedMethodInvocationExpression(fieldInfoReference, typeof(ReferenceType).GetMethod("MethodWithArgs"), new ConstReference(1).ToExpression(), new ConstReference("2").ToExpression())); Assert.That(InvokeMethod(), Is.EqualTo(new Tuple <int, string> (1, "2"))); }
public void TryFinallyWithoutException() { FieldReference tryField = ClassEmitter.CreateField("TryExecuted", typeof(bool)); FieldReference finallyField = ClassEmitter.CreateField("FinallyExecuted", typeof(bool)); var methodEmitter = GetMethodEmitter(false, typeof(void), new Type[0]); Statement[] tryBlock = new Statement[] { new AssignStatement(tryField, new ConstReference(true).ToExpression()) }; Statement[] finallyBlock = new Statement[] { new AssignStatement(finallyField, new ConstReference(true).ToExpression()) }; methodEmitter.AddStatement(new TryFinallyStatement(tryBlock, finallyBlock)); methodEmitter.AddStatement(new ReturnStatement()); InvokeMethod(); Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), tryField.Reference.Name), Is.True); Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), finallyField.Reference.Name), Is.True); }
protected new void CreateInterceptorsField(ClassEmitter emitter) => emitter.CreateField("__interceptors", Options.InterceptorType.MakeArrayType());
protected new void CreateInterceptorsField(ClassEmitter emitter) { emitter.CreateField("__interceptors", typeof(ComputedServiceInterceptor[])); }
private FieldReference BuildTargetField(ClassEmitter @class, Type type) { var name = "__mixin_" + type.FullName.Replace(".", "_"); return(@class.CreateField(namingScope.GetUniqueName(name), type)); }