Re: return object of an unknown type
- From: "Allan Ebdrup" <ebdrup@xxxxxxxxxxxxxxx>
- Date: Thu, 25 Jan 2007 14:24:25 +0100
"Alexander Widera" <awid@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:%23qMJ0lHQHHA.4104@xxxxxxxxxxxxxxxxxxxxxxx
thanks for your answer, but i think this issn't the result of my problem
i don't want to make a usual cast ...
in a variable should be the type of an object and a method should return
an other object as this type.
perhaps i have to specifize the situation:
i have 2 classes ... MyMainClass and MyClass2 ... MyClass2 is derived of
MyMainClass
in MyMainClass there is an Method like this
public virtual OtherObject GetData()
{
// .. a lot work to do...
return (OtherType)privateObject);
}
privateObject is a variable of the type "object" and the method returns it
casted to "OtherType".
in MyClass2 this Method should not return the privateObject casted to
"OtherType" ... it should return it casted to "AnotherType"...
because there is much to do in this method (not shown above), i don't want
to rewrite the method completely for every child-class of MyMainClass...
Are you using C# 2.0? If so you could use a generic method:
public virtual T GetData<T>()
{
// .. a lot work to do...
return (T)privateObject;
}
And call GetData<OtherType>() or GetData<AnotherType>()
Kind Regards,
Allan Ebdrup
.
- Follow-Ups:
- Re: return object of an unknown type
- From: Bobbo
- Re: return object of an unknown type
- References:
- return object of an unknown type
- From: Alexander Widera
- Re: return object of an unknown type
- From: DeveloperX
- Re: return object of an unknown type
- From: Alexander Widera
- return object of an unknown type
- Prev by Date: Re: creating a ram drive in csharp
- Next by Date: Casting of generics?
- Previous by thread: Re: return object of an unknown type
- Next by thread: Re: return object of an unknown type
- Index(es):
Loading