说我有一个返回类型的方法:
private Type GetPersonOrOrganisation(someParameter)
{
either return Person type or Organisation type
}
然后我称之为这个方法:
Type type = GetPersonOrOrganisation(someParameter);
然后我尝试创建一个返回类型的新实例:
var newContact = Activator.CreateInstance(type);
我留下的是newContact是object类型。我想要的是newContact属于Person或Organization类型,具体取决于GetPersonOrOrganisation返回的内容。
有谁知道如何将newContact强制转换为正确的类型?
答案 0 :(得分:1)
这绝对有一些代码味道。但也许有一些方法可以解决它。
如果要以相同的方式与他们进行交互,您可能需要考虑人员和组织实施的界面。或者可能是基类,具体取决于您的具体情况。
除此之外,我们可能需要您之后尝试做的事情才能提出建议。如果没有接口(或其他一些基类),您就不能拥有一个可以是这些类型的对象。他们目前唯一的共同点是对象。
根据您的情况,您可以执行一些不同的操作,例如if (newContact is Person) { } else if (newContact is Organisation) { }
或类似的操作,但除非您完全坚持使用这些对象和方法,否则它们会真正进入代码的味道是
答案 1 :(得分:1)
您可以从函数返回初始化对象,并使用GetType()和typeof对其进行测试。下面是一个例子(当然蒂姆的例子也会起作用)。
public class Person
{
}
public class Organization
{
}
class Program
{
// Generate a Person if i == true or Organization if i == false
static object GetPersonOrOrganization(bool i)
{
if (i == true)
{
return new Person();
}
else
{
return new Organization();
}
}
static void Main(string[] args)
{
var p = GetPersonOrOrganization(true); // Generates a Person.
if (p.GetType() == typeof(Person))
{
Console.WriteLine("Person!"); // This prints.
}
else
{
Console.WriteLine("Organization");
}
var o = GetPersonOrOrganization(false); // Generates an Organization.
if (o.GetType() == typeof(Person))
{
Console.WriteLine("Person!");
}
else
{
Console.WriteLine("Organization!"); // This prints.
}
Console.ReadLine();
}
}
答案 2 :(得分:1)
你需要这样的东西:
public interface IPersonOrganization {
}
public class Peron : IPersonOrganization {
}
public class Organization : IPersonOrganization {
}
private IPersonOrganization GetPersonOrganization(bool isPerson) {
if (isPerson)
return new Person();
else
return new Organization;
}
答案 3 :(得分:0)
这是一种方式;虽然这假定存在无参数构造函数:
using System;
namespace StackOverflow.Demos
{
class Program
{
public static void Main(string[] args)
{
new Program();
Console.ReadKey();
}
private Program()
{
Type type = GetPersonOrOrganisation(new Person());
//object myInstance = GetInstanceOfType(type);
var myInstance = GetInstanceOfType(type);
Console.WriteLine(myInstance.ToString());
type = GetPersonOrOrganisation(new Organization());
myInstance = GetInstanceOfType(type);
Console.WriteLine(myInstance.ToString());
}
private Type GetPersonOrOrganisation(object what)
{
return what.GetType();
}
private object GetInstanceOfType(Type type)
{
//return type.GetConstructor(new Type[] { }).Invoke(new object[] { });
return Activator.CreateInstance(type);
}
}
public class Person
{
public Person() { }
}
public class Organization
{
public Organization() { }
}
}
答案 4 :(得分:0)
这是你遇到的问题吗?
public void Demo()
{
var myInstance = Activator.CreateInstance((new Person()).GetType());
Console.WriteLine(Test(myInstance));
}
private string Test(object x) //this is the method being called
{
return string.Format("Object - {0}", x.ToString());
}
private string Test(Person x) //this is what you were hoping for
{
return string.Format("Person - {0}", x.ToString());
}
private string Test(Organization x)
{
return string.Format("Org - {0}", x.ToString());
}
这是一个解决方案(不推荐):
public void Demo()
{
var myInstance = Activator.CreateInstance((new Person()).GetType());
Console.WriteLine(Test(myInstance));
}
private string Test(object x) //redirect the call to the right method
{
if (x is Person)
return Test(x as Person);
else
return Test(x as Organization);
}
private string Test(Person x) { return string.Format("Person - {0}", x.ToString()); } //this is what you were hoping for
private string Test(Organization x) { return string.Format("Org - {0}", x.ToString()); }
更好的解决方案是:
public interface ITestMethod { string Test();}
public class Person : ITestMethod
{
public Person() { }
public string Test() { return string.Format("Person - {0}", this.ToString()); }
}
public class Organization : ITestMethod
{
public Organization() { }
public string Test() { return string.Format("Org - {0}", this.ToString()); }
}
//...
public void Demo()
{
var myInstance = Activator.CreateInstance((new Person()).GetType()) as ITestMethod;
Console.WriteLine(myInstance.Test());
}
//...