473,498 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Checked == true)
{
WebReference.WebClass o = new WebReference.WebClass();
o.Method;
}
else
{
ComponentReference y = new ComponentReference();
y.Method();
}

--WebReference, WebClass and ComponentReference 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.WebClass();
or
object o = new ComponentReference();

but that failed.

This also failed:

if (chkUseWS.Checked == true)
{
WebReference.WebClass o = new WebReference.WebClass();
}
else
{
ComponentReference o = new ComponentReference();
}

o.Method();

Is there a way to do what I'm trying to do?
Jul 21 '05 #1
1 1365
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.LoadFile("assemblyPath").GetType("classNa me");' There are
optimizations you can do around this, but this code should help you get
started.

using System;

using System.Reflection;

namespace ConsoleApplication1

{

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("ConsoleApplication1.ClassA");

obj = new ClassA();

}

else

{

type = Type.GetType("ConsoleApplication1.ClassB");

obj = new ClassB();

}

// More info on InvokeMember at

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

string result = (string) type.InvokeMember("GetString", BindingFlags.Default
| BindingFlags.InvokeMethod, null, obj, null);
Console.WriteLine(result);

}

}

}

"Doug" <dn******@dtgnet.com> wrote in message
news:ff**************************@posting.google.c om...
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.Checked == true)
{
WebReference.WebClass o = new WebReference.WebClass();
o.Method;
}
else
{
ComponentReference y = new ComponentReference();
y.Method();
}

--WebReference, WebClass and ComponentReference 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.WebClass();
or
object o = new ComponentReference();

but that failed.

This also failed:

if (chkUseWS.Checked == true)
{
WebReference.WebClass o = new WebReference.WebClass();
}
else
{
ComponentReference o = new ComponentReference();
}

o.Method();

Is there a way to do what I'm trying to do?

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12539
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
8
4812
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
6
1906
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
7
6751
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
3
1072
by: Daniel Friend | last post by:
I have an application and use custom user controls as plugins. Is there any way to communicate back and forth from control to app. NOTE: The control is not refrenced in the app, it will act as a...
5
2008
by: SalamElias | last post by:
I am creating several chkBoxes dynamically and assigning an event handler in the Page_load as foillows ***************************** Dim chkCatOption As CheckBox = New CheckBox chkCatOption.Text...
6
1473
by: Bjorn Sagbakken | last post by:
Hello In VS2005: I am adding buttons and textboxes dynamically into a table, that also dynamically expands. So far, so good, actually very nice. But I am having trouble starting the desired...
6
5443
by: RSH | last post by:
Hi, i have a situation where I need to dynamically create objects in a loop. My question surrounds intantiation naming in such a scenerio. Below is a snippet that is basically hardcoding each...
10
2550
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
4
2952
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.