views:

42

answers:

2

I am passing in a dynamic type into a method and having some issues running the code. Wondering if you are able to pass a dynamic object into as a parameter using the out keyword.

Below is the code.

dynamic btApp = AutomationFactory.CreateObject("Test.Application");
dynamic btMessages;

dynamic btFormat = btApp.Formats.Open("c:\\Temp/Format1.btw", false, "");
btFormat.SetNamedSubStringValue("testing", "testtest");
btFormat.Print("Job1", true, -1, out btMessages);
btFormat.Close(2);

issue is in the print method. where the last argument is passing in a dynamic object.

A: 

It depends on what the actual type signature of the Print method is. The dynamic type is represented as object at runtime, so if the Print method takes an out parameter of type object (or dynamic), then it should work.

If the Print method has actual out parameter of some other type, then the actual runtime type used at the side of the caller doesn't match the actual type of the declaration, so it will not work.

Tomas Petricek
I am trying to get to the print method but I seem to be struggling with that right now. I was able to get in there today at some point and I saw that the first 3 parameters are what is expected, the last method is of type Message message...if I remember correctly the Message type was tied to a message interface..Im sure this doesnt help, but I remember that the method didnt take a ref parameter or of a dynamic type..I will still try and get into the sdk to see the actual code but wanted to respond before the end of the evening..Thanks for the help...
gevjen
also..since this is a com object i was able to build a WPF app and use the same code without any errors or issues. I need it to run in a silverlight application. I cannot reference a COM object in the SL app, so I am doing it this way. This line of code that is giving me trouble(the print method) worked perfectly fine when it ran inside a wpf app.
gevjen
+1  A: 

When you pass a out parameter to a method with a variable that is of type dynamic the parameter itself must be of type dynamic. The following code is legal:

class Program {
    static void Main(string[] args) {
        dynamic value;
        SomeMethod(out value);
        return;
    }
    static void SomeMethod(out dynamic value) {
        value = "5";
        return;
    }
}

In fact SomeMethod can assign anything to value. When the parameter is not of type dynamic then the compiler attempts to convert before the method call, which is not permitted, so if the parameter in SomeMethod is anything but dynamic, your out of luck.

Steve Ellinger
Thanks Steve. to go a bit further with my issue. I am building a silverlight app. I have a com object I am trying to reference. I cannot reference a com object in my SL project. so to test the code I was working on. I built a wpf application and wired things up so it would work(print). Once I knew that code worked, I transferred that code to my silverlight app using the dynamic keyword to get a handle on the com object. That print line in my code worked in my wpf app, but does not work when i transferred to my SL app..not sure if that helps but thought i would throw it in...thanks for the help
gevjen
here is the code from the print method.[DispId(48)] [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] BtPrintResult Print([MarshalAs(UnmanagedType.BStr), In] string PrintJobName = "", [In] bool WaitForSpoolJobToComplete = true, [In] int TimeoutMs = -1, [MarshalAs(UnmanagedType.Interface)] out Messages Messages = null);
gevjen
That last parameter is [MarshalAs(UnmanagedType.Interface)] out Messages Messages = null)...so it has the out keyword..but its of type Message...
gevjen