locked
Creating OutArgument from property type RRS feed

  • Question

  • Dear all, 

    Is that possible to create an instance of OutArgument given the Type of the object?

    Here is the code behind this questions:

    Activity activityReference = VisualBasicDesignerHelper.CreatePrecompiledVisualBasicReference(
      null,
      ToExpression,
      _namespaces,
      _assemblies,
      _varLocRef,
      out returnType,
      out compileErrorMessage,
      out vbSettings);
    
    if (compileErrorMessage != null)
    {
      return ((System.Exception)(compileErrorMessage)).Message;
    }
    
    //I can create the OutArgument instance using this method if I know the exact type
    //OutArgument myOutArgument = new OutArgument<someType>((VisualBasicReference<someType>)(activityReference));
    
    //However, in my case above, I want to use returnType instance to determine the type and create the appropriate OutArgument instance
    

    Regards,

     


    arinto
    Tuesday, July 12, 2011 8:41 AM

Answers

  • Yes, you can first build the generic type using reflection, then instantiate it using Activator.CreateInstance().

    Type returnType = /*...*/;
    
    Type outArgT = typeof(OutArgument<>).MakeGenericType(returnType);
    
    OutArgument myOutArgument = Activator.CreateInstance(outArgT, /*constructor params if necessary*/)
    

     Tim


    • Proposed as answer by Tim Lovell-Smith Tuesday, July 12, 2011 8:21 PM
    • Marked as answer by Andrew_Zhu Wednesday, July 20, 2011 12:45 AM
    Tuesday, July 12, 2011 8:21 PM

All replies

  • Yes, you can first build the generic type using reflection, then instantiate it using Activator.CreateInstance().

    Type returnType = /*...*/;
    
    Type outArgT = typeof(OutArgument<>).MakeGenericType(returnType);
    
    OutArgument myOutArgument = Activator.CreateInstance(outArgT, /*constructor params if necessary*/)
    

     Tim


    • Proposed as answer by Tim Lovell-Smith Tuesday, July 12, 2011 8:21 PM
    • Marked as answer by Andrew_Zhu Wednesday, July 20, 2011 12:45 AM
    Tuesday, July 12, 2011 8:21 PM
  • Thanks Tim!
    arinto
    Wednesday, August 3, 2011 9:39 AM