1

假设我有一个返回类型的方法:

private Type GetPersonOrOrganisation(someParameter)
{
  either return Person type or Organisation type
}

然后我调用这个方法:

Type type = GetPersonOrOrganisation(someParameter);

然后我尝试创建返回类型的新实例:

var newContact = Activator.CreateInstance(type);

我剩下的是 newContact 是对象类型。我想要的是 newContact 是 Person 或 Organization 类型,具体取决于从 GetPersonOrOrganisation 返回的内容。

有谁知道如何将 newContact 转换为正确的类型?

4

5 回答 5

1

这肯定有一些代码味道。但也许有一些方法可以解决它。

如果您要以相同的方式与个人和组织进行交互,您可能需要考虑一个界面,个人和组织都实现了它们。或者可能是一个基类,具体取决于您的具体情况。

除此之外,我们可能需要您之后尝试做的事情才能提供建议。如果没有接口(或其他一些基类),您就不能拥有可以是其中任何一种类型的 ONE 对象。他们目前唯一的共同点是对象。

你可以根据你的场景做一些不同的事情,比如if (newContact is Person) { } else if (newContact is Organisation) { }或类似的事情,但这真的会进入代码异味,除非你完全被这些对象和方法所困。

于 2013-03-13T03:06:14.693 回答
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();
    }
}
于 2013-03-13T03:06:30.227 回答
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;
}
于 2013-03-13T03:08:49.837 回答
0

Here's one way; though this assumes a parameterless constructor exists:

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() { }
    }
}
于 2013-03-13T03:17:21.027 回答
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());
}
//...
于 2013-03-13T04:01:26.587 回答