I have written the dynamic web services for SOAP and REST. Now need to create OData web services that has operations that return IQueryable Interface as per WCF data service specification. I am creating a dynamic method based on IL Emit statements. But am not able to assign the return type to the MethodBuilder which is IQueryable.
To be specific, I am not able to set the return parameter using MethodBuilder.SetReturnType Method as the return type IQueryable is generated at run time.
public class MainClass
{
public static void Main()
{
AssemblyBuilder serviceAsmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyDynamicAsm"), AssemblyBuilderAccess.RunAndSave);
ModuleBuilder serviceModule = serviceAsmBuilder.DefineDynamicModule("MyDynamicAsm", "MyDynamicAsm.dll");
TypeBuilder serviceTypeBuilder = serviceModule.DefineType("MyDynamicType", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass,
typeof(DataService<WebServerResult>) );
//This is the problem, as I do not have this class at compile
//time. I get this class at run time
Type returnType = typeof(IQueryable<WebServerResult>);
MethodBuilder MethodBldr = serviceTypeBuilder.DefineMethod("OperationName", MethodAttributes.Public, returnType, Type.EmptyTypes);
ILGenerator methodGenerator = MethodBldr.GetILGenerator();
methodGenerator.Emit(OpCodes.Ret);
}
}
//This is just for reference. This code does not exist at design time
[DataContract]
public class WebServerResult
{
[DataMember]
public List<Variable> Variables { get; set; }
public IQueryable<Variable> ODataResultList
{
get { return Variables.AsQueryable<Variable>(); }
}
}
[DataServiceKey("VariableName")]
[DataContract(Namespace = "")]
public class Variable
{
[DataMember]
public string VariableName { get; set; }
[DataMember]
public string VariableValue { get; set; }
[DataMember]
public string VariableType { get; set; }
}
Can someone provide me the blue print of the method generation in IL emit code?