Casting a derived class to base class

  • Thread starter Thread starter user
  • Start date Start date
U

user

I want to get from ChildClass object an object of type BaseClass, how
can I do that ?
i need this conversion for existing code becouse a webService accepts
only BaseClass.

here is the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testDerivedBaseClass
{
public class BaseClass
{
public string s;
public int i;
public BaseClass()
{
;
}
}

public class ChildClass : BaseClass
{
public string child1;
public ChildClass()
{

}
}


class Program
{
static void Main(string[] args)
{
ChildClass C1 = new ChildClass();
BaseClass B1 = (BaseClass)C1;
string t = B1.GetType().ToString();
//t = "testDerivedBaseClass.ChildClass"
B1 = null;
B1 = C1 as BaseClass;
t = B1.GetType().ToString();
//t = "testDerivedBaseClass.ChildClass"
Console.Write(B1.i);
}
}
}


//////////////////////////////////////

I also made a function to copy object:
but i dont want to create copy filling memory.

/////////////////////////////////////


public static T1 CopyObject<T1, T2>(T2 obj)
where T1 : new()
{
T1 objNew = new T1();
Type classType = obj.GetType();
PropertyInfo[] arrProp = classType.GetProperties();
foreach (PropertyInfo p in arrProp)
{
if (p.CanWrite)
{
if ((objNew.GetType()).GetProperty(p.Name) != null)
{
p.SetValue(objNew, p.GetValue(obj, null), null);
}
}
}
return objNew;
}
 
user laid this down on his screen :
I want to get from ChildClass object an object of type BaseClass, how
can I do that ?
i need this conversion for existing code becouse a webService accepts
only BaseClass.

here is the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testDerivedBaseClass
{
public class BaseClass
{
public string s;
public int i;
public BaseClass()
{
;
}
}

public class ChildClass : BaseClass
{
public string child1;
public ChildClass()
{

}
}


class Program
{
static void Main(string[] args)
{
ChildClass C1 = new ChildClass();
BaseClass B1 = (BaseClass)C1;
string t = B1.GetType().ToString();
//t = "testDerivedBaseClass.ChildClass"
B1 = null;
B1 = C1 as BaseClass;
t = B1.GetType().ToString();
//t = "testDerivedBaseClass.ChildClass"
Console.Write(B1.i);
}
}
}

By casting (whether you use "(BaseClass)" or "as BaseClass") you get
the ChildClass to *act* like a BaseClass.

Try to access the public field "child1" that is defined only in
ChildClass: that will not work through variable "B1", as that will only
expose the fields, properties and methods that are defined in
BaseClass.

A method that accepts a BaseClass as parameter, should also work when
you supply it with a ChildClass, unless weird things are going on in
there.

Hans Kesting
 
Hi

It sounds like the requirement revolves around the serialization
'appearance' of your object / entity to the web service.

You might have a look at using the XmlSerializer (or
DataContractSerializer for WCF) in order to send just the base class
properties?

But this would then require a very low level of interaction with the
web service.

Otherwise, as Peter says, create a cloning method / operator which
copies across just the base properties.
You can probably do this more easily / generically by using the
Serializers

where obj is a ChildClass instance

XmlSerializer serializer = new XmlSerializer(typeof(BaseClass));
StringWriter sw = new StringWriter(sb);
serializer.Serialize(sw, obj);

and then deserialize it back to the BaseClass ?

Regards

Stuart
 
Back
Top