0

Say I am working with the following code:

Type type = info.ParameterType;
object activatedTypeToReference = Activator.CreateInstance(type.GetElementType());

How do I create a reference parameter object to the above activatedTypeToReference object in C#?

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • Just pass it. In c# everything is passed by reference. – Odys Jan 31 '13 at 19:38
  • 3
    @odyodyodys: No, far from it. Please read http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Jan 31 '13 at 19:39
  • http://stackoverflow.com/a/3040202/245495 – Odys Jan 31 '13 at 19:39
  • 5
    What are you actually trying to achieve? It's not clear what you mean by "reference parameter object" to start with - if you could give the bigger picture, it would be much easier to help you. – Jon Skeet Jan 31 '13 at 19:39
  • Its really complicated because I am working on a dynamic web service invoker which takes a WSDL and creates a ServiceDescription which is used to create a "proxy" (for lack of a better term) to a WCF service by compiling an assembly of it. The problem is that some service methods have out types and when they do, I need to be able to handle them dynamically, so some functions will require me to create an instance of a reference type parameter and pass that in. One solution I found was to use the ParameterModifier class to allow me to invoke by reference types, but I would rather not use that. – Alexandru Jan 31 '13 at 19:56
  • When you say "reference parameter" are you referring to a parameter that utilizes the `ref` keyword? – Leon Newswanger Jan 31 '13 at 19:58
  • @LeonNewswanger - Well, when you have a method like public void Test6(out CarConditionEnum Test6Result, [System.Xml.Serialization.XmlIgnoreAttribute()] out bool Test6ResultSpecified) ...when you call this method it expects one of its types to be of type CarConditionEnum& as opposed to just CarConditionEnum, so that is what I am referring to. I am not sure exactly what else to call this type...except a type reference? – Alexandru Jan 31 '13 at 20:01
  • @JonSkeet - Duly noted, I have tried my best to create a bigger picture in my comment above, I'm sorry to everyone if my problem description wasn't straightforward, I tried my best to get to the simplest case I had at hand. – Alexandru Jan 31 '13 at 20:02
  • 1
    @odyodyodys: Yes, I expressed myself on that answer too... I'm not sure what your point is. – Jon Skeet Jan 31 '13 at 20:05
  • So, is your question how you would dynamically handle `out` parameters in cases where you don't know the type? @JonSkeet I couldn't help but wonder why the link led to another one of your answers either... – Leon Newswanger Jan 31 '13 at 20:06

1 Answers1

2

When you invoke the method, you pass in an array of arguments. For an out parameter, you don't need to specify anything for the array element - the value can just be null. When the method returns, the array will contain the value set by the method. Here's an example:

using System;

public class Test
{
    static void Main()
    {        
        var method = typeof(Test).GetMethod("DummyMethod");
        object[] args = new object[1];
        method.Invoke(null, args);
        Console.WriteLine(args[0]); // Prints 10
    }

    public static void DummyMethod(out int x)
    {
        x = 10;
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks, Jon, I forgot that out types aren't usable by methods, such that you can null them out and check the arguments after invocation to see their set values after the method returns. This makes the problem much simpler. – Alexandru Jan 31 '13 at 20:23
  • Sorry to resurrect skeletons, but I had a thought. What if this method explicitly took in a pointer to a type I'd only know at run-time (aside from an output parameter, supposing this was an input parameter)? Is that even possible in an unsafe context for WCF services? If so, would I not need to somehow pass in a pointer reference to the activated object? If so, how? – Alexandru Jan 31 '13 at 20:53
  • 1
    @Alexandru: I'm not sure what you mean. Are you *really* talking about pointer types? It's hard to know what that would even mean in the context of WCF. – Jon Skeet Jan 31 '13 at 20:55
  • I'm not sure what the exact term is. Perhaps you could tell me, if I have the following OperationContract on my WCF service, then is this (the int* integer object) really a pointer type or type reference, or what? [OperationContract] void Test9(int* integer); – Alexandru Jan 31 '13 at 20:59
  • So if all I know about the method on run-time is that it takes in a certain type (in this case int*) from its ParameterInfo, how can I create an instance of this type and pass that into the method as part of args when calling method.Invoke? The trouble for me in this case is that it would not be a simple element type. – Alexandru Feb 01 '13 at 13:33
  • 1
    @Alexandru: Well as soon as you get into *pointer* types, you're in a world of pain anyway, IMO. But it feels odd to have that on a WCF service anyway, to be honest. It's certainly beyond the realms of my knowledge. – Jon Skeet Feb 01 '13 at 13:59
  • Thanks Jon, I agree with you. I think I am trying to go too far in my attempt to support all types of WCF services for what I am doing. I think it is bad practice from what I've read to take pointer types as arguments, as this would require letting the service run in an unsafe context, so I will opt not to support it. – Alexandru Feb 01 '13 at 14:32
  • 1
    @Alexandru: Sounds sensible. I don't know whether WCF even supports such a service, to be honest. If you assume it will be accessed remotely (over any kind of channel) the concept of "pointer" doesn't make very much sense. – Jon Skeet Feb 01 '13 at 14:34
  • If you build it with the "Allow unsafe code" option and use the unsafe keyword before declaring the interface method, it will build. But as for getting it to run or create its metadata, it may not work at all, so this may not be a case from where I was trying to go with my question. Thanks, Jon. – Alexandru Feb 01 '13 at 14:58