Dynamic casting and dynamic variables.

  • Thread starter Martin Hart - Memory Soft, S.L.
  • Start date
M

Martin Hart - Memory Soft, S.L.

Hi all:

I still very new to the .NET world and don't know if what I am asking is due
to an over-imaginative imagination or the fact that I have read too many
fiction books!!

Let me show you a very basic scenario:

[Example]
using System;

namespace DynamicCasts
{
public class BaseClass
{
public int SharedInt;
public BaseClass()
{
}
}

public class DerivedClass1 : BaseClass
{
public int Int1;
public DerivedClass1()
{
}
}

public class DerivedClass2 : BaseClass
{
public int Int2;
public DerivedClass2()
{
}
}

public class Test
{
[STAThread]
static void Main()
{
DerivedClass1 dc1 = new DerivedClass1();
DerivedClass2 dc2 = new DerivedClass2();
DoSomeThing(dc1);
DoSomeThing(dc2);
}

static public void DoSomeThing(BaseClass bc)
{
Type myType = bc.GetType();
// How can I do dynamic casting?
if(myType.Name == "DerivedClass1")
((??)bc).Int1 = 33; //----------Here!!
else if(myType.Name == "DerivedClass2")
((??)bc).Int2 = 33; //----------Here!!

// Can I also define a variable based on myType's type and then
manipulate the variable?

}
}
}
[/Example]

I am looking for a way to dynamically cast the BaseClass variable to its
derived type and assign a value to one of its fields.

Secondly, through Reflection, can I define a variable based on the Type of
the object passed to the method (i.e. DerivedClass1 or DerivedClass2) and
then change the Int1 or Int2 field based on this variable?

Thanks for your time,

Martin Hart
Memory Soft
 
C

Chris Ellis

Martin,

I tested the code below. It works and it seems to do what you require,
if I read you right. When executed, it produces the following output.

d1Ctrl = 333, 25
d1Xper = 7, 137
d2Ctrl = 333, 50
d2Xper = 7, 777

here's the code:

class MainClass
{
[STAThread]
static void Main(string[] args)
{
Der1 d1Ctrl = new Der1(),
d1Xper = new Der1();
Der2 d2Ctrl = new Der2(),
d2Xper = new Der2();

Test( d1Xper );
Test( d2Xper );

c.WriteLine( "d1Ctrl = {0}", d1Ctrl );
c.WriteLine( "d1Xper = {0}", d1Xper );
c.WriteLine( "d2Ctrl = {0}", d2Ctrl );
c.WriteLine( "d2Xper = {0}", d2Xper );
}

static void Test( BaseClass bc )
{
// look at the docs on "as" and "is" keywords
Der1 d1 = bc as Der1;
Der2 d2 = bc as Der2;

if( d1 != null )
d1.m_one = 137;
else
d2.m_two = 777;

bc.m_shared = 7;
}
}


abstract class BaseClass
{
public int m_shared = 333;

public override string ToString()
{
return m_shared.ToString();
}
}

class Der1: BaseClass
{
public int m_one = 25;

public override string ToString()
{
return string.Format( "{0}, {1}", base.ToString (), m_one.ToString() );
}
}

class Der2: BaseClass
{
public int m_two = 50;

public override string ToString()
{
return string.Format( "{0}, {1}", base.ToString (), m_two.ToString() );
}
}
 
M

Martin Hart - Memory Soft, S.L.

Hi Chris:

Thanks for the feedback. What I was hoping for was a way to cast the 'bc'
parameter that arrives at the Test method to its native type (via GetType??)
and use this to cast the variable in stead of using multiple 'bc as ???'
statements. I have a case where I can have a dozen or so possible classes
and wanted to reduce the multiple 'if' statements.

The pseudo code below is what I was hoping might exist:

static void Test( BaseClass bc )
{
Type t = bc.GetType();

((t)bc).m_generic = 33; //---Note this syntax is NOT correct!!
}
}

[snip]

class Der1: BaseClass
{
public int m_generic = 25; //---Note the field m_generic also exists in
class Der2
}

class Der2: BaseClass
{
public int m_generic = 50; //---Note the field m_generic also exists in
class Der1
}

I think this might be in the realm of dreams! Your thoughts would be
appreciated.

Regards,
Martin.
 
J

Jon Skeet [C# MVP]

<"Martin Hart - Memory Soft, S.L." <memorysoftsl _at_ infotelecom
_dot_ es> said:
Thanks for the feedback. What I was hoping for was a way to cast the 'bc'
parameter that arrives at the Test method to its native type (via GetType??)
and use this to cast the variable in stead of using multiple 'bc as ???'
statements. I have a case where I can have a dozen or so possible classes
and wanted to reduce the multiple 'if' statements.

The thing to do is not have public fields in the first place (which is
a bad idea all round) and to expose a property in an interface instead.
The various classes could implement the interface, and then you only
need to cast to the interface and use the property it exposes.

(Or put the property in the base class you've got in this case.)
 

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