You may just be able to do this if your types have built in conversions
int i = Convert.Int32(MyFunction(...))
string s = Convert.ToString(MyFunction(...))
If you have more complicated conversions, but a limited number of return
types, you can do it without an explicit cast. Instead of returning
"object", create a new class for return type that knows how to convert
object to whatever you want. An example is below that defines the class
MultiType and uses it for string and int.
If you want MyFunction() to return an "object" and not a MultiType
instance, you can do
int i = new MultiType(MyFunction(...,"System.Int32"));
and if you want to specify what you are converting to, you can do
int i = new MultiType("System.Int32",
MyFunction(...,"System.Int32"));
Or you can add a static method Cast() to MultiType that
int i = MultiType.Cast("System.Int32",
MyFunction(...,"System.Int32"));
Cast() just needs to instantiate a MultiType().
There are many other things you can do, even handling types that are not
known at compile time (e.g. come from dynamically selected/loaded
assemblies). In the later case, you may need to require that the
assembly provides the necessary conversions depending on exactly what
types of conversions you are doing.
using System;
namespace casttest
{
class MultiType
{
object o;
public MultiType(object o) { this.o = o;}
static public implicit operator System.Int32(MultiType m)
{
return int.Parse(m.o.ToString());
// or
// return (int)o;
// if you know it is an int
}
static public implicit operator System.String(MultiType m)
{
return m.o.ToString();
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
MultiType MyFunction(object oin,string typename)
{
object o = null;
if (typename.Equals("System.Int32"))
{
o = (int)oin;
}
else if (typename.Equals("System.String"))
{
o = (string)oin;
}
return new MultiType(o);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
int i = c.MyFunction(1,"System.Int32");
string s = c.MyFunction("text","System.String");
Console.WriteLine("results " + i + " " + s);
}
}
}
mra wrote:
Hi, Nicholas, unfortunately, its not so easy.
In reality, I have a function that returns an object. What kind of object
this is (class) is defined by the typename string:
object MyFunction ([...parameters], typename);
If you call this function as indicated, it will return an integer:
object obj=MyFunction([...parameters], "System.Int32");
I want to do something like:
int i=MyFunction([...parameters], "System.Int32");
This does not work, because you cannot assign an object to an int.
Ideally, there would be a possibility to say:
int i=(runtime type cast to int)MyFunction([...parameters], "System.Int32");
where the (runtime type cast) should take a string with the typename as an
argument. Any idea?
"Nicholas Paldino [.NET/C# MVP]" wrote:
mra,
If you have a reference to the type, then yes, you can, just cast the
return value like you would normally.
My assumption is that you do not have a reference to that type, and that
you are loading the type through a call to one of the Load overloads on the
Assembly class.
In this case, you will want to define an interface or a base class in
another assembly, and have your type implement that. Then, in the assembly
that loads and creates the type, you cast the return value to the interface,
and make your calls.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"mra" <mr*@discussions.microsoft.com> wrote in message
news:D5**********************************@micros oft.com...
I want to cast an object that I have created from a typename to the
corresponding type. Can anycone tell me how to do this?
Example:
//Here, Create the object of type "MyClass"
object obj=Activator.CreateInstance(strAssemblyName, "MyClass");
//Now, I want to do something like ((MyClass)obj).Method
//Can I do this?