Example #1
0
		CodeMethod (CodeClass cls, MethodAttributes attributes, Type[] parameterTypes) 
		{
			this.cls = cls;
			this.typeBuilder = cls.TypeBuilder;
			this.attributes = attributes;
			this.parameterTypes = parameterTypes;
			this.name = typeBuilder.Name;
		
			methodBase = typeBuilder.DefineConstructor (attributes, CallingConventions.Standard, parameterTypes);
			builder = new CodeBuilder (cls);
		}
Example #2
0
		internal CodeMethod (CodeClass cls, string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) 
		{
			this.cls = cls;
			this.typeBuilder = cls.TypeBuilder;
			this.name = name;
			this.attributes = attributes;
			this.returnType = returnType;
			this.parameterTypes = parameterTypes;
		
			methodBase = typeBuilder.DefineMethod (name, attributes, returnType, parameterTypes);
			builder = new CodeBuilder (cls);
		}
		internal CodeProperty (CodeClass cls, string name, PropertyAttributes attributes, MethodAttributes methodAttributes, Type returnType, Type[] parameterTypes) 
		{
			this.cls = cls;
			this.typeBuilder = cls.TypeBuilder;
			this.name = name;
			this.attributes = attributes;
			this.methodAttributes = methodAttributes;
			this.returnType = returnType;
			this.parameterTypes = parameterTypes;
		
			PropertyBuilder pb = typeBuilder.DefineProperty (name, attributes, returnType, parameterTypes);
			pb.SetGetMethod (typeBuilder.DefineMethod ("get_" + name, methodAttributes, CallingConventions.Standard, returnType, Type.EmptyTypes));
			pb.SetSetMethod (typeBuilder.DefineMethod ("set_" + name, methodAttributes, CallingConventions.Standard, typeof (void), new Type [] {returnType}));
			get_builder = new CodeBuilder (cls);
			set_builder = new CodeBuilder (cls);
			propertyInfo = pb;
		}
		protected static Type CreateProxyTypeOperations (Type crtype, CodeClass c, ContractDescription cd)
		{
			// member implementation
			BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
			foreach (OperationDescription od in cd.Operations) {
				// FIXME: handle properties and events.
#if !NET_2_1
				if (od.SyncMethod != null)
					GenerateMethodImpl (c, crtype.GetMethod ("Process", bf), od.Name, od.SyncMethod);
#endif
				if (od.BeginMethod != null)
					GenerateBeginMethodImpl (c, crtype.GetMethod ("BeginProcess", bf), od.Name, od.BeginMethod);
				if (od.EndMethod != null)
					GenerateEndMethodImpl (c, crtype.GetMethod ("EndProcess", bf), od.Name, od.EndMethod);
			}

			//Type zzz = c.CreateType ();
			//((System.Reflection.Emit.AssemblyBuilder) zzz.Assembly).Save (modname + ".dll");
			//return zzz;
			return c.CreateType ();
		}
Example #5
0
		public static CodeMethod DefineConstructor (CodeClass cls, MethodAttributes attributes, Type[] parameterTypes)
		{
			return new CodeMethod (cls, attributes, parameterTypes);
		}
Example #6
0
		internal static CodeMethod DefineMethod (CodeClass cls, string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
		{
			return new CodeMethod (cls, name, attributes, returnType, parameterTypes);
		}
		internal static CodeProperty DefineProperty (CodeClass cls, string name, PropertyAttributes attributes, MethodAttributes methodAttributes, Type returnType, Type[] parameterTypes)
		{
			return new CodeProperty (cls, name, attributes, methodAttributes, returnType, parameterTypes);
		}
Example #8
0
		static void GenerateEndMethodImpl (CodeClass c, MethodInfo endProcessMethod, string name, MethodInfo mi)
		{
			CodeMethod m = c.ImplementMethod (mi);
			CodeBuilder b = m.CodeBuilder;
			ParameterInfo [] pinfos = mi.GetParameters ();

			ParameterInfo p = pinfos [0];
			CodeArgumentReference asyncResultRef = m.GetArg (0);
			
			CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration (typeof (object []), "parameters");
			b.CurrentBlock.Add (paramsDecl);
			CodeVariableReference paramsRef = paramsDecl.Variable;
			b.Assign (paramsRef,
				  new CodeNewArray (typeof (object), new CodeLiteral (pinfos.Length - 1)));
			/*
			for (int i = 0; i < pinfos.Length - 2; i++) {
				ParameterInfo par = pinfos [i];
				if (!par.IsOut)
					b.Assign (
						new CodeArrayItem (paramsRef, new CodeLiteral (i)),
						new CodeCast (typeof (object),
							new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i)));
			}
			*/
#if USE_OD_REFERENCE_IN_PROXY
			CodePropertyReference argMethodInfo = GetOperationMethod (m, b, name, "EndMethod");
#else
			CodeMethodCall argMethodInfo = new CodeMethodCall (typeof (MethodBase), "GetCurrentMethod");
#endif
			CodeLiteral argOperName = new CodeLiteral (name);
			
			CodeVariableReference retValue = null;
			if (mi.ReturnType == typeof (void))
				b.Call (m.GetThis (), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef);
			else {
				CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration (mi.ReturnType, "retValue");
				b.CurrentBlock.Add (retValueDecl);
				retValue = retValueDecl.Variable;
				b.Assign (retValue,
					new CodeCast (mi.ReturnType,
						b.CallFunc (m.GetThis (), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef)));
			}
			// FIXME: fill out parameters
			if (retValue != null)
				b.Return (retValue);
		}
Example #9
0
		static void GenerateBeginMethodImpl (CodeClass c, MethodInfo beginProcessMethod, string name, MethodInfo mi)
		{
			CodeMethod m = c.ImplementMethod (mi);
			CodeBuilder b = m.CodeBuilder;
			// object [] parameters = new object [x];
			// parameters [0] = arg1;
			// parameters [1] = arg2;
			// ...
			// (return) BeginProcess (Contract.Operations [operName].BeginMethod, operName, parameters, asyncCallback, userState);
			ParameterInfo [] pinfos = mi.GetParameters ();
			CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration (typeof (object []), "parameters");
			b.CurrentBlock.Add (paramsDecl);
			CodeVariableReference paramsRef = paramsDecl.Variable;
			b.Assign (paramsRef,
				  new CodeNewArray (typeof (object), new CodeLiteral (pinfos.Length - 2)));
			for (int i = 0; i < pinfos.Length - 2; i++) {
				ParameterInfo par = pinfos [i];
				if (!par.IsOut)
					b.Assign (
						new CodeArrayItem (paramsRef, new CodeLiteral (i)),
						new CodeCast (typeof (object), m.GetArg (i)));
			}
#if USE_OD_REFERENCE_IN_PROXY
			CodePropertyReference argMethodInfo = GetOperationMethod (m, b, name, "BeginMethod");
#else
			CodeMethodCall argMethodInfo = new CodeMethodCall (typeof (MethodBase), "GetCurrentMethod");
#endif
			CodeLiteral argOperName = new CodeLiteral (name);

			ParameterInfo p = pinfos [pinfos.Length - 2];
			CodeArgumentReference callbackRef = new CodeArgumentReference (typeof (AsyncCallback), p.Position + 1, p.Name);
			p = pinfos [pinfos.Length - 1];
			CodeArgumentReference stateRef = new CodeArgumentReference (typeof (object), p.Position + 1, p.Name);

			CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration (mi.ReturnType, "retValue");
			b.CurrentBlock.Add (retValueDecl);
			CodeVariableReference retValue = retValueDecl.Variable;
			b.Assign (retValue,
				new CodeCast (mi.ReturnType,
					b.CallFunc (m.GetThis (), beginProcessMethod, argMethodInfo, argOperName, paramsRef, callbackRef, stateRef)));

			b.Return (retValue);
		}
Example #10
0
		static void GenerateMethodImpl (CodeClass c, MethodInfo processMethod, string name, MethodInfo mi)
		{
			CodeMethod m = c.ImplementMethod (mi);
			CodeBuilder b = m.CodeBuilder;
			// object [] parameters = new object [x];
			// parameters [0] = arg1;
			// parameters [1] = arg2;
			// ...
			// (return) Process (Contract.Operations [operName].SyncMethod, operName, parameters);
			ParameterInfo [] pinfos = mi.GetParameters ();
			CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration (typeof (object []), "parameters");
			b.CurrentBlock.Add (paramsDecl);
			CodeVariableReference paramsRef = paramsDecl.Variable;
			b.Assign (paramsRef,
				  new CodeNewArray (typeof (object), new CodeLiteral (pinfos.Length)));
			for (int i = 0; i < pinfos.Length; i++) {
				ParameterInfo par = pinfos [i];
				if (!par.IsOut)
					b.Assign (
						new CodeArrayItem (paramsRef, new CodeLiteral (i)),
						new CodeCast (typeof (object),
							new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i)));
			}
#if USE_OD_REFERENCE_IN_PROXY
			CodePropertyReference argMethodInfo = GetOperationMethod (m, b, name, "SyncMethod");
#else
			CodeMethodCall argMethodInfo = new CodeMethodCall (typeof (MethodBase), "GetCurrentMethod");
#endif
			CodeLiteral argOperName = new CodeLiteral (name);
			CodeVariableReference retValue = null;
			if (mi.ReturnType == typeof (void))
				b.Call (m.GetThis (), processMethod, argMethodInfo, argOperName, paramsRef);
			else {
				CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration (mi.ReturnType, "retValue");
				b.CurrentBlock.Add (retValueDecl);
				retValue = retValueDecl.Variable;
				b.Assign (retValue,
					new CodeCast (mi.ReturnType,
						b.CallFunc (m.GetThis (), processMethod, argMethodInfo, argOperName, paramsRef)));
			}
			for (int i = 0; i < pinfos.Length; i++) {
				ParameterInfo par = pinfos [i];
				if (par.IsOut || par.ParameterType.IsByRef)
					b.Assign (
						new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i),
						new CodeCast (par.ParameterType.GetElementType (),
							new CodeArrayItem (paramsRef, new CodeLiteral (i))));
			}
			if (retValue != null)
				b.Return (retValue);
		}
Example #11
0
		protected static Type CreateProxyTypeOperations (Type crtype, CodeClass c, ContractDescription cd)
		{
			// member implementation
			BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
			foreach (OperationDescription od in cd.Operations) {
				// FIXME: handle properties and events.
#if !NET_2_1
				if (od.SyncMethod != null)
					GenerateMethodImpl (c, crtype.GetMethod ("Process", bf), od.Name, od.SyncMethod);
#endif
				if (od.BeginMethod != null)
					GenerateBeginMethodImpl (c, crtype.GetMethod ("BeginProcess", bf), od.Name, od.BeginMethod);
				if (od.EndMethod != null)
					GenerateEndMethodImpl (c, crtype.GetMethod ("EndProcess", bf), od.Name, od.EndMethod);
			}

			Type ret = c.CreateType ();
			return ret;
		}
		public CodeBuilder (CodeClass codeClass)
		{
			this.codeClass = codeClass;
			mainBlock = new CodeBlock ();
			currentBlock = mainBlock;
		}