Dynamically creating an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug

    #1

    Dynamically creating an object

    I am attempting to create a unit test that can switch between calling
    a method from the web service I created or calling the component that
    the web service actually calls.

    So in my unit test form I have a checkbox that I can use to "turn on
    or off" which one I would call and I have code something like this:

    if (chkUseWs.Check ed == true)
    {
    WebReference.We bClass o = new WebReference.We bClass();
    o.Method;
    }
    else
    {
    ComponentRefere nce y = new ComponentRefere nce();
    y.Method();
    }

    --WebReference, WebClass and ComponentRefere nce are all made up
    names... :)

    Anyhow, I'd like to instead just create one variable and assign it to
    either the web reference or the component reference based on whether
    the checkbox is checked.

    I've tried creating an object variable like this:

    object o = new WebReference.We bClass();
    or
    object o = new ComponentRefere nce();

    but that failed.

    This also failed:

    if (chkUseWS.Check ed == true)
    {
    WebReference.We bClass o = new WebReference.We bClass();
    }
    else
    {
    ComponentRefere nce o = new ComponentRefere nce();
    }

    o.Method();

    Is there a way to do what I'm trying to do?
  • Ed Kaim [MSFT]

    #2
    Re: Dynamically creating an object

    Here's some code that lets you call a method with the same signature on two
    unrelated classes. ClassA and ClassB are random, but they could apply to
    your business object and the Web service proxy object generated in the
    project. If you're loading object types from other assemblies, you'll have
    to do something like 'type =
    Assembly.LoadFi le("assemblyPat h").GetType("cl assName");' There are
    optimizations you can do around this, but this code should help you get
    started.

    using System;

    using System.Reflecti on;

    namespace ConsoleApplicat ion1

    {

    class ClassA

    {

    public ClassA() {}

    public string GetString()

    {

    return "ClassA";

    }

    }

    class ClassB

    {

    public ClassB() {}

    public string GetString()

    {

    return "ClassB";

    }

    }



    /// <summary>

    /// Summary description for Class1.

    /// </summary>

    class Class1

    {

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main(string[] args)

    {

    bool useClassA = true;

    Type type;

    object obj;

    if (useClassA)

    {

    type = Type.GetType("C onsoleApplicati on1.ClassA");

    obj = new ClassA();

    }

    else

    {

    type = Type.GetType("C onsoleApplicati on1.ClassB");

    obj = new ClassB();

    }

    // More info on InvokeMember at

    //
    http://msdn.microsoft.com/library/de...embertopic.asp

    string result = (string) type.InvokeMemb er("GetString" , BindingFlags.De fault
    | BindingFlags.In vokeMethod, null, obj, null);


    Console.WriteLi ne(result);

    }

    }

    }

    "Doug" <dnlwhite@dtgne t.com> wrote in message
    news:ff7d6992.0 312311429.76de2 1ae@posting.goo gle.com...[color=blue]
    > I am attempting to create a unit test that can switch between calling
    > a method from the web service I created or calling the component that
    > the web service actually calls.
    >
    > So in my unit test form I have a checkbox that I can use to "turn on
    > or off" which one I would call and I have code something like this:
    >
    > if (chkUseWs.Check ed == true)
    > {
    > WebReference.We bClass o = new WebReference.We bClass();
    > o.Method;
    > }
    > else
    > {
    > ComponentRefere nce y = new ComponentRefere nce();
    > y.Method();
    > }
    >
    > --WebReference, WebClass and ComponentRefere nce are all made up
    > names... :)
    >
    > Anyhow, I'd like to instead just create one variable and assign it to
    > either the web reference or the component reference based on whether
    > the checkbox is checked.
    >
    > I've tried creating an object variable like this:
    >
    > object o = new WebReference.We bClass();
    > or
    > object o = new ComponentRefere nce();
    >
    > but that failed.
    >
    > This also failed:
    >
    > if (chkUseWS.Check ed == true)
    > {
    > WebReference.We bClass o = new WebReference.We bClass();
    > }
    > else
    > {
    > ComponentRefere nce o = new ComponentRefere nce();
    > }
    >
    > o.Method();
    >
    > Is there a way to do what I'm trying to do?[/color]


    Comment

    Working...