【问题标题】:Type manipulation类型操作
【发布时间】:2013-09-19 16:26:26
【问题描述】:

是否可以创建一个返回类型并使用它的方法?

例如,假设我有一个 Person 类型和 Object 参数。 你可能知道,如果我们想转换我们的 Object 参数,我们可以这样写:

object param;

((Person)param).executePersonMethod();

我的问题是我如何编写一个返回 Person 类型的方法并使用它来代替具体的 Person 强制转换 - 我想写这样的东西:

public Type GetPersonType()
{
//return here type of person
}

然后使用

  ((GetPersonType())param).executePersonMethod();

有可能吗? 如果是,怎么做?

【问题讨论】:

标签: c# types casting


【解决方案1】:

你可以使用界面。

((IPerson)param).executePersonMethod();

每种类型的人都必须是 IPerson 并在 IPerson 中声明 executePersonMethod()

【讨论】:

  • 嗨,我知道这种方法 - 它不适合我的场景,我需要更多动态解决方案。我不想为任何类型创建单独的接口
【解决方案2】:

您也可以为此使用dynamic

请注意,使用dynamic 将跳过任何编译时检查该方法是否存在,如果不存在则在运行时抛出异常。

由于这种风险,我只有在别无选择的情况下才会这样做,但很高兴知道该选项存在:

dynamic d = param;
d.executeWhateverMethodHereWithoutCompileTimeChecking();

【讨论】:

    【解决方案3】:

    是的,你可以。有一种新的类型叫做 dynamic,它可以避免编译过程中的静态类型检查。

    public dynamic GetPersonOrObjectWhichHasExecutePersonMethod()
    {
     //return not the type but the object itself
     return new Person(); 
    }
    
    public class Person
    {
        public void executePersonMethod()
        {
          // do something
        }
    }
    
    // this is how you invoke it
    public void ExecuteMethod()
    {
      dynamic obj = GetPersonOrObjectWhichHasExecutePersonMethod();
      obj.executePersonMethod();
    }
    

    【讨论】:

      【解决方案4】:

      也许你可以使用类似的东西:

      Convert.ChangeType(param, typeof(Person)); 
      

      它将参数作为一个人返回。

      【讨论】:

        【解决方案5】:

        不能Type 上执行方法,您只能在特定Type 的实例上执行方法。如果我理解正确,您应该能够使用 @LavG 给出的答案:

        GetPersonType 方法返回的不是类型而是对象本身

        编辑:根据您的评论:

        • 这里有一些 SO QA 可以帮助您充分利用 Type 限定命名空间和其他技术:

        How to get the type for a class by sending just the name of the class instead of the class itself as the parameter?

        Type.GetType("namespace.a.b.ClassName") returns null

        • 以下是如何在运行时从给定类型生成类:

        generating a class dynamically from types that are fetched at runtime

        【讨论】:

          【解决方案6】:

          使用

          TypeOf()

          例子:

                  if (data.GetType() == typeof(Personne))
                      return (Personne)data;
                  else
                      return new Personne();
          

          之后,检查您的对象是否为空,以了解是否正常

          【讨论】:

            【解决方案7】:

            虽然您可以使用 Generic 作为更好的选择,但也可以将类型转换器与反射结合使用:

            Type type = GetPersonType();
            var converted = Convert.ChangeType(param, type);
            converted
                 .GetType()
                 .GetMethod(/*your desired method name in accordance with appropriate bindings */)
                 .Invoke(converted, /* your parameters go here */);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-30
              • 2020-08-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-06-17
              相关资源
              最近更新 更多