![]() ![]() Post navigation ← Increasing the maximum number of connections with maxconnection Compiling C# code at runtime → This entry was posted in C# on Januby purpleblob. MakeGenericType(unknown1.GetType(), unknown2.GetType()) Obviously this is assuming T2 actually does something of use in our code.Īll of the previous sample code will work, the difference is that we declare the typeof(Runner) as typeof(Runner) and need to pass the extra parameters in the MakeGenericType method, for example What about when we have more than one generic parameter Next we use the same type but with the GetMethod call and ofcourse pass the instance variable into Invoke. The first time we use it is with Activator.CreateInstance this creates an instance of the class with a generic parameter. In the above we need to use the genericType twice, so we store it in a local variable. Var runner = Activator.CreateInstance(genericType) Ofcourse we can’t simply create an instance to this class and use the same techniques of previous because the type of the generic parameter is not known, so we need to create an instance of this class via reflection then invoke the method – this can be accomplished with ![]() Obviously we might wish to create non-static classes with generic parameters as well, something like this ![]() Notice how the MakeGenericType is used to generate our generic class and the syntax of the typeof. So now we need to make the generic on the type not the method. So now let’s move the generic parameter onto the class itself. To invoke this Create method we can simply write ![]() So this assumes Create is an extension method for the class, DoSomething. Public static T Create(this DoSomething doSomething) The only real difference to the code for the instance method on a non-static class is that we do not pass a instance to the first parameter of the invoke method, like thisĪs extension methods expect a “this” argument, they’re no different to the above code, expect that we need to ensure the first argument is the instance of an object So the only real difference here is that we also need to pass the type into the method, a simple addition of the parameters to the invoke will allow this to work, here’s the codeĬalling methods on a static class or extension methodsĪs you will know, extension methods are really just static classes with syntactic sugar to allow them to appear like instance methods, so the procedure for invoking them is the same as the normal static classed, but we’ll cover examples here all the same. Next up, let’s look at the same code but where we need to also pass the method a generic parameter. Note: we can ofcourse use typeof(Runner) or runner.GetType() in the above depending upon your preference or use. This code would also work if the Create method was a static. Object unknown = CreateType() // generates some type at runtime So let’s assume we have some code like thisĪnd we want to invoke this at runtime with an “unknown” (i.e. Let’s take a look at the syntax of various scenarios and you’ll get the idea. For example, situation where the type is not known at compiler time but the method(s) we want to use expect the generic parameter to be supplied. Sometimes we need to run methods or create types with generic parameters at runtime. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |