472,779 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

Pass type to method to allow create and method invocation?

Hello,

I have a base class with several derived classes:
ScriptBase
+ ScriptCallInbound
+ ScriptCallOutbound

I then have another class:
Line

Line has a method:
DoExecute()

What I want DoExecute to do be passed a type (ScriptCallInbound or
ScriptCallOutbound) and be able to call an Execute() method defined in the
ScriptBase as virtual and supplied by the derived classes.

So, I assume the DoExecute() method would need to accept some kind of type
of a ScriptBase derived class that it could then create a new instance of
and invoke the Execute() method. I want both the NEW and Execute() methods
to be handled by the actual type passed.

How do I go about doing this in C#?

Thanks,

Grant Schenck
Aug 17 '07 #1
9 3413
Hi,

"Grant Schenck" <sc******@optonline.netwrote in message
news:OO****************@TK2MSFTNGP03.phx.gbl...
Hello,

I have a base class with several derived classes:
ScriptBase
+ ScriptCallInbound
+ ScriptCallOutbound

I then have another class:
Line

Line has a method:
DoExecute()

What I want DoExecute to do be passed a type (ScriptCallInbound or
ScriptCallOutbound) and be able to call an Execute() method defined in the
ScriptBase as virtual and supplied by the derived classes.

The method can use a ScriptBase as its parameter, then as the method is
virtual it will execute the correct version based on the type of the
instance being passed.
Aug 17 '07 #2

On Aug 17, 4:17 pm, DeveloperX <nntp...@operamail.comwrote:
public void DoExecute(Type pType)
If you want compile-time type safety, as well as some cuddly framework
comfort, you could make DoExecute a generic method like so:

public void DoExecute<TScript>() where TScript : TScriptBase

Also, passing the Type of an instance to DoExecute() and then creating
a new instance sounds like throwing one instance away. Perhaps passing
the actual instance to DoExecute() in the first place would be
applicable here.

Aug 17 '07 #3
On 17 Aug, 15:27, UL-Tomten <tom...@gmail.comwrote:
On Aug 17, 4:17 pm, DeveloperX <nntp...@operamail.comwrote:
public void DoExecute(Type pType)

If you want compile-time type safety, as well as some cuddly framework
comfort, you could make DoExecute a generic method like so:

public void DoExecute<TScript>() where TScript : TScriptBase

Also, passing the Type of an instance to DoExecute() and then creating
a new instance sounds like throwing one instance away. Perhaps passing
the actual instance to DoExecute() in the first place would be
applicable here.
I'm in the office at the moment, so only got 1.1 :(

Would that work? I read that as TScript must implement TScriptBase,
but we're passing in a class of type Type which does not.
My knowledge might be muddied by the fact that it's a type of type
Type and Type might expose something that where understands...

Aug 17 '07 #4
Well, just to muddy the water, I want to create an instance of the passed
type and be able to pass parameters. In my example, Script is an abstract
base class and Execute() is abstract.

So, I want to be able to do something like:

// Script's constructor
Script(int nID, string nName)
{...

// ScriptInbound Derived from
ScriptInbound : Script
{
public Override void Execute()
{...

ScriptOutbound : Script

DoExecute(Type pType, int nID, string nName)
{
// Not sure how I do...
Script script = new pType(nID, nName);

script.Execute();
}

THANKS!
--
Grant Schenck
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On 17 Aug, 15:27, UL-Tomten <tom...@gmail.comwrote:
>On Aug 17, 4:17 pm, DeveloperX <nntp...@operamail.comwrote:
public void DoExecute(Type pType)

If you want compile-time type safety, as well as some cuddly framework
comfort, you could make DoExecute a generic method like so:

public void DoExecute<TScript>() where TScript : TScriptBase

Also, passing the Type of an instance to DoExecute() and then creating
a new instance sounds like throwing one instance away. Perhaps passing
the actual instance to DoExecute() in the first place would be
applicable here.

I'm in the office at the moment, so only got 1.1 :(

Would that work? I read that as TScript must implement TScriptBase,
but we're passing in a class of type Type which does not.
My knowledge might be muddied by the fact that it's a type of type
Type and Type might expose something that where understands...

Aug 17 '07 #5

On Aug 17, 4:59 pm, DeveloperX <nntp...@operamail.comwrote:
Would that work? I read that as TScript must implement TScriptBase,
but we're passing in a class of type Type which does not.
Well, I would certainly recommend against passing a Type, because it
seems like something you would only do if late binding was your only
option. I got the impression your class hierarchy and its uses was
known at runtime, in which case the compiler and the CLR could do all
that work for you. But since I haven't the faintest clue about what
the bigger picture is, maybe a Type is what you need.
My knowledge might be muddied by the fact that it's a type of type
Type and Type might expose something that where understands...
No, I was only assuming a usage along the lines of, say:

DoExecute<ScriptCallInbound>(myScriptCallInbound);

Instead of passing a Type to DoExecute() and creating an instance
inside the method, wouldn't it make more sense to create an instance
outside the method and pass that?

Aug 17 '07 #6
On 17 Aug, 17:32, UL-Tomten <tom...@gmail.comwrote:
On Aug 17, 4:59 pm, DeveloperX <nntp...@operamail.comwrote:
Would that work? I read that as TScript must implement TScriptBase,
but we're passing in a class of type Type which does not.

Well, I would certainly recommend against passing a Type, because it
seems like something you would only do if late binding was your only
option. I got the impression your class hierarchy and its uses was
known at runtime, in which case the compiler and the CLR could do all
that work for you. But since I haven't the faintest clue about what
the bigger picture is, maybe a Type is what you need.
My knowledge might be muddied by the fact that it's a type of type
Type and Type might expose something that where understands...

No, I was only assuming a usage along the lines of, say:

DoExecute<ScriptCallInbound>(myScriptCallInbound);

Instead of passing a Type to DoExecute() and creating an instance
inside the method, wouldn't it make more sense to create an instance
outside the method and pass that?
Oh ok, I didn't realise you were re-architecting it :) Speaking of
which I sort of agree with you, but without knowing the OP's reasons
for wanting to create the object in DoExecute we can't really say. If
it's possible to create it outside it certainly makes sense for the
reasons you''ve listed above.

Grant, I'm on a train at the moment, but from memory CreateInstance
has overloads which allow arrays of parameters to be passed in. If you
can't get it to work I'll take a look as soon as I can if someone else
doesn't get there before me :)
Aug 17 '07 #7
That's fine, I have it working without parms by using a base initialize
method. I can look at passing parms.

I'm certainly open to a better mouse trap on this.

My GOAL is as follows:

I'm developing a class library for an phone system IVR (interactive voice
response.) It is based on a COM object which internally uses the PBX's TAPI
Interface.

The DoExecute() method on the base class creates a thread, associates it
with the class and then calls an Execute() method provided by the derived
class. This method can then carry out the work of the IVR "script" in a
synchronous fashion.

The idea is that I can define an IVR script which is run when a new call
presents and can then answer, play audio, capture digits, transfer, etc the
call. This way the thread can block for each stage of the IVR to complete.

The individual stages (answer call, play audio to caller and collect digits,
etc.) all block waiting for an event while the underlying COM object carries
them out. These stages are coded as methods in the base class and are used
by the derived classes execute methods to actually do work.

SO... I want to package the class as a separate C# library and then be able
to link with the libary from typically a Windows Service app and be able to
just create a class derived from Script with an Execute method to do the
actual work of the script.

Hope that makes sense...?
--
Grant Schenck
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
On 17 Aug, 17:32, UL-Tomten <tom...@gmail.comwrote:
>On Aug 17, 4:59 pm, DeveloperX <nntp...@operamail.comwrote:
Would that work? I read that as TScript must implement TScriptBase,
but we're passing in a class of type Type which does not.

Well, I would certainly recommend against passing a Type, because it
seems like something you would only do if late binding was your only
option. I got the impression your class hierarchy and its uses was
known at runtime, in which case the compiler and the CLR could do all
that work for you. But since I haven't the faintest clue about what
the bigger picture is, maybe a Type is what you need.
My knowledge might be muddied by the fact that it's a type of type
Type and Type might expose something that where understands...

No, I was only assuming a usage along the lines of, say:

DoExecute<ScriptCallInbound>(myScriptCallInbound);

Instead of passing a Type to DoExecute() and creating an instance
inside the method, wouldn't it make more sense to create an instance
outside the method and pass that?

Oh ok, I didn't realise you were re-architecting it :) Speaking of
which I sort of agree with you, but without knowing the OP's reasons
for wanting to create the object in DoExecute we can't really say. If
it's possible to create it outside it certainly makes sense for the
reasons you''ve listed above.

Grant, I'm on a train at the moment, but from memory CreateInstance
has overloads which allow arrays of parameters to be passed in. If you
can't get it to work I'll take a look as soon as I can if someone else
doesn't get there before me :)


Aug 17 '07 #8
No because the class needs to be able to create instances of the specified
type in response to external events (in this case, phone calls.)

So, this class is to monitor one line or port of a phone system's
interactive voice response unit. The class wants to respond to inbound
calls by creating a script object to interact with the caller (the script
spins off a thread) FOR EACH CALL.
--
Grant Schenck

"UL-Tomten" <to****@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>
On Aug 17, 4:59 pm, DeveloperX <nntp...@operamail.comwrote:
>Would that work? I read that as TScript must implement TScriptBase,
but we're passing in a class of type Type which does not.

Well, I would certainly recommend against passing a Type, because it
seems like something you would only do if late binding was your only
option. I got the impression your class hierarchy and its uses was
known at runtime, in which case the compiler and the CLR could do all
that work for you. But since I haven't the faintest clue about what
the bigger picture is, maybe a Type is what you need.
>My knowledge might be muddied by the fact that it's a type of type
Type and Type might expose something that where understands...

No, I was only assuming a usage along the lines of, say:

DoExecute<ScriptCallInbound>(myScriptCallInbound);

Instead of passing a Type to DoExecute() and creating an instance
inside the method, wouldn't it make more sense to create an instance
outside the method and pass that?

Aug 17 '07 #9

On Aug 17, 9:06 pm, "Grant Schenck" <schen...@optonline.netwrote:
Instead of passing a Type to DoExecute() and creating an instance
inside the method, wouldn't it make more sense to create an instance
outside the method and pass that?

No because the class needs to be able to create instances of the specified
type in response to external events (in this case, phone calls.)
It still sounds like you could use type safe instance creation, like:

DoExecute<TScriptCall>() where TScriptCall : ScriptCallBase {
TScriptCall scriptCall = new TScriptCall();
scriptCall.OverriddenMethod();
}

....and invoke it thusly:

DoExecute<ScriptCallInbound>();

My point is that compile-time type safety in my experience usually
saves nine.

Aug 17 '07 #10

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: Alex Sedow | last post by:
Method invocation will consist of the following steps: 1. Member lookup (14.3) - evaluate method group set (Base.f() and Derived.f()) .Standart say: "The compile-time processing of a method...
8
by: MrEd | last post by:
Hi all, I need to pass a class to a method, (not an instance of a class), I'll explain... lets say we have a class, lets call it Shape (as the tipical example), then lets say that i also have...
1
by: cognominal | last post by:
I am using fifrefox 1.5.0.2 and I have modified the javascript shell to deal with e4x. Below is a session. I would have expected <toto/>.nodeKind to be of type "function". Is this right? Is...
0
by: gchandran | last post by:
Hello, I am developing a DLL using C# . I am using VS 2005 for this. The component will expose few API's. One of the API performs certain operations and needs to return an array of objects...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.