dynamic type at runtime?

N

netnet

I have a asp.net page with a method to track down a dynamic control....

private void findAndSetTheControl(Control theControl, object theType, string
nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}


It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}



What I would like to do is dynamically type (theType) it at runtime. Is
this possible without using some sort of abstract class and doing the
kabookie dance? I know that GetType() wiill return the objects type but im
not really sure how to use it at runtime.



Thanks in advance for the feedback.


Sean
 
C

chris martin

I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheControl(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}
It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}

What I would like to do is dynamically type (theType) it at runtime.
Is this possible without using some sort of abstract class and doing
the kabookie dance? I know that GetType() wiill return the objects
type but im not really sure how to use it at runtime.

Thanks in advance for the feedback.

Sean

Since you know the type of the control in your calling method, why not just
return the control from FindTheControl()
and cast it in your calling method?

Control FindTheControl(string nameOfControl)
{
return PH.FindControl(nameOfControl);
}

void FindAndSetThePaymentControl()
{
PaymentControl control = FindTheControl("paymentControl") as PaymentControl;

...yada yada yada...and no kabookie dance.
 
N

Nicholas Paldino [.NET/C# MVP]

Sean,

All you could do is use reflection, and trust that the methods have the
name and signature you want. Of course, this is a horrific practice, and
you should just use an interface which all of the objects implement.

Hope this helps.
 
N

netnet

Thanks for the quick feedback guys. Good point Chris. Smalltalk has ruined
me, some things are just sooooo much simpler. Nicholas, are you feelings
on reflection due to the performance problems I have read about? It doesnt
really seem like there is a clean way to a number of things in C#? I havent
found a really nice way to do a vistor pattern without it? If anyone has an
example please share.

Thanks again.

Sean
 
N

Nicholas Paldino [.NET/C# MVP]

Sean,

Well, performance has something to do with it, but it's not just that.

Basically, you have an expectation that these objects should fufill, and
that is most easily defined by an interface. This also allows you to
perform compile-time checks to ensure that anyone calling your method abides
by the contract. This is VASTLY better than having it blow up in your face
at run-time.
 
C

chris martin

Thanks for the quick feedback guys. Good point Chris. Smalltalk has
ruined me, some things are just sooooo much simpler. Nicholas, are
you feelings on reflection due to the performance problems I have read
about? It doesnt really seem like there is a clean way to a number of
things in C#? I havent found a really nice way to do a vistor pattern
without it? If anyone has an example please share.

Thanks again.

Sean


http://www.dofactory.com/Patterns/PatternVisitor.aspx#_self2

On that page there is a nice, clean example of the Visitor pattern in .NET
or any OOL at that.

Chri
 
N

netnet

Thanks for the link Chris. Good point Nicholas. This is an example of the
concept I seem to be having a tough time w in C#:

Payment
-amount

Check : Payment
-checkNumber

CreditCard : Payment
-ccNumber
-ccExpiration

Now when I go to use this class structure .....I have to create a container,
populate it, AND all methods have to be implemented on payment at least as
abstracts...which seems like a lot of work.

I dont really need Payment or Check to know anything about ccExpiration for
example.

This is how I have been doing it ...Please let me know if you have cleaner
approach


Payment[] aPayment = new Payment[1];

if(someCondition)
aPayment[0] = new CreditCard(info);
else
aPayment[0] = new Check(info);


Theres a better way right?

Thanks again

Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top