【问题标题】:C# - Convert String to Class ObjectC# - 将字符串转换为类对象
【发布时间】:2011-09-30 21:07:10
【问题描述】:

我正在使用其他人的代码并尝试进行一些修改。所以我需要做的是采取以下措施:

RemoteFileDP remoteFile = new DPFactory().CreateRemoteFileDP(configData);

并更改它,以便 remoteFile 可以等于字符串变量中的内容。为了进一步解释,让我提供更多代码:

ConfigDP configData = new ConfigDP();

所以上面的语句在remoteFile语句之前执行,并且ConfigDP上面有两个类(abstract Config,然后是它的base:abstract ConfigBase)。 DP 也是它上面的两个抽象类(抽象 RemoteFile 和抽象 RemoteFileBase)的子类。

据我了解,remoteFile 是从数据库查询中提取的数据的结果,存储到列表或哈希表中(抱歉,只是一个实习生,所以我正在处理这个问题)。

我需要 remoteFile 来接受字符串值的原因是因为有许多方法可以利用 remoteFile 中的信息,我想避免创建一个接受字符串值而不是 RemoteFileDP remoteFile 的全部重载方法。

所以如果我可以取一个字符串值,比如:

string locationDirectory;

它是从另一个方法传入的,然后有类似下面的东西:

RemoteFileDP remoteFile = locationDirectory;

那么使用remoteFile 的所有其他方法将不必重载或更改。

抱歉所有细节,但这是我第一次发帖,所以我希望我提供了足够的信息。我确实查看了C# Convert dynamic string to existing Class 和C#: Instantiate an object with a runtime-determined type 并编写了以下代码:

RemoteFilesDP remoteFile = (RemoteFileDP)Activator.CreateInstance(typeof(RemoteFileDP), locationDirectory);

但是我不断收到“MissingMethodException”错误,即找不到 RemoteFileDP 的构造函数,但我确实有如下所示的构造函数:

public RemoteFileDP()
    {
    } //end of RemoteFilePlattsDP constructor

提前感谢您的帮助!

【问题讨论】:

  • 也许这有帮助? stackoverflow.com/questions/208777/…这些你试过了吗?
  • 感谢您的回复。我现在就去看看!
  • 打开链接后,我看到这是我发布问题后一直在审查的链接。我试过FormatterServices.GetUninitializedObject,但它对我不起作用。表示没有接受两个参数的重载。
  • 奇怪,它在哪里说的?编译器错误,运行时错误?具体在哪里?
  • @ can poyrazoğlu: 它在编译时给出错误。这是我使用的代码:'RemoteFileDP remoteFile = (RemoteFileDP)FormatterServices.GetUninitializedObject(typeof(RemoteFileDP), locationDirectory);'

标签: c# string class


【解决方案1】:

虽然我更喜欢构造函数接受string 的想法,但您可以在RemoteFileDP 和string 之间定义一个隐式或显式转换运算符:

 class RemoteFileDP
 {
      ....

      public static implicit operator RemoteFileDP(string locationDictionary)
      {
           //return a new and appropiately initialized RemoteFileDP object.
           //you could mix this solution with Anna's the following way:
           return new RemoteFileDP(locationDictionary);
      }
 }

这样你实际上可以写:

 RemoteFileDP remoteFile = locationDirectory;

或者,如果转换运算符是显式的:

 RemoteFileDP remoteFile = (RemoteFileDP)locationDirectory;

我仍然坚持认为,Anna Lear 的解决方案更好,因为隐式或显式转换似乎并不是最适合这种情况。例如,如果转换可能由于无效的locationDictionary 值而失败,那么我不会推荐此路径。如果无论locationDictionary 是什么值(除了null),转换总是成功的,那么它可能是解决您问题的有效方法。

我只是把它放在桌面上,因为我认为您可能会发现了解 C# 中的 explicit 和 implicit 转换很有用,如果您还没有的话。

【讨论】:

  • 感谢您的回复!我确实先尝试了显式转换(必须先研究它),但似乎无法让它工作。所以我选择了 Activator.CreateInstance 的另一条路径,因为这看起来更像是我想要的。但是,是的,我总是乐于学习新事物。
  • @Invaderleige:发布的所有解决方案基本相同。最后,您必须在给定某个string 的情况下正确初始化RemoteFileDP 对象。每次从字符串初始化 RemoteFileDP 时,转换运算符只会为您节省几次击键。 (关于如何将 Anna 的解决方案与转换运算符集成,请参阅我的答案的编辑)。
  • 感谢您的更新并展示了我如何将 Anna 的解决方案与您的解决方案一起使用。
【解决方案2】:

如果您不想修改RemoteFileDP 所在的(或不能)源项目,您可以编写如下扩展方法:

public static RemoteFileDP ConvertToRemoteFileDP(this string location)
{
    // Somehow create a RemoteFileDP object with your 
    // location string and return it
}

这样你就可以运行你想要的代码行:

RemoteFileDP remoteFile = locationDirectory;

稍作修改如下:

RemoteFileDP remoteFile = locationDirectory.ConvertToRemoteFileDP();

这可以解决您的问题吗?

【讨论】:

  • 嗯...我得试试这个。这可能需要一些工作,因为 RemoteFileDP 是一些抽象类的子类,所以我必须确保所有这些抽象类都具有相同的抽象方法,但我会尝试并让你知道。
  • +1。这是最好的方法,因为您不能将字符串隐式转换为类,也不能重载 = 运算符。
  • 所以我现在正在处理这个问题,但由于我还是个新手,我将不得不弄清楚如何使代码 cmets 中的部分发生。使用位置字符串创建 RemoteFileDP 对象。但我会在完成后通知你
  • 作为一般规则,请尝试查看其他通过某种用户或文件输入构建 RemoteFileDP 对象的地方,然后按照他们在那里使用的模式进行操作。
  • 因此,我根据“Anna Lear”提供的建议完成了代码的编写工作,并且成功了。对不起,我先接近了她,这更容易弄清楚。但是我喜欢学习不同的做事方式,并不总是想采取简单的方法。所以我仍在研究你的,会通知你的。
【解决方案3】:

您缺少一个将string 作为参数的构造函数。用

试试你的代码
public RemoteFileDP(string locationDirectory)
{
    // do stuff with locationDirectory to initialize RemoteFileDP appropriately
}

当然,如果你这样做,为什么不直接调用构造函数呢?

RemoteFileDP remoteFile = new RemoteFileDP(locationDirectory);

【讨论】:

  • 嗯,这也可以。有时似乎简单的事情被忽略了。我也会试试这个,让你知道。
  • 是的,这有效!谢谢“安娜李尔”。现在我需要尝试提供的其他方法。
  • @Invaderleige 很高兴听到这个消息。 :) 如果您最终采用这种方法,请单击我对accept it 的回答左侧的复选标记。
猜你喜欢
  • 2017-02-07
  • 1970-01-01
  • 2022-01-01
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多