C#-将字符串转换为类对象 [英] C# - Convert String to Class Object

查看:1039
本文介绍了C#-将字符串转换为类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用其他人的代码,并尝试进行一些修改。因此,我需要做的是:

I am working with someone else's code and trying to make some modifications. So what I'm needing to do is take the following:

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

并进行更改,以便remoteFile可以等于字符串变量中的值。为了进一步解释,让我给出更多代码:

And change it so that remoteFile can equal what is in a string variable. To further explain let me give some more of the code:

ConfigDP configData = new ConfigDP();

因此,以上语句在remoteFile语句之前执行,并且ConfigDP在其上面有两个类(抽象Config然后是它的基础:抽象ConfigBase)。 DP还是它上面两个抽象类的子类(抽象RemoteFile和抽象RemoteFileBase)。

So the statement above is executed before the remoteFile statement and ConfigDP is has two classes above it (abstract Config and then its base: abstract ConfigBase). DP is also the child of two abstract classes above it (abstract RemoteFile and abstract RemoteFileBase).

据我所知,remoteFile是从数据库查询中提取数据的结果,存储到列表或哈希表中(很抱歉,只是一名实习生,所以我正在研究中)。

From my understanding remoteFile is the result of data extracted from a database query, stored into a list or a Hashtable (sorry just an intern so I'm working through this).

之所以需要remoteFile接受字符串值,是因为有很多利用remoteFile中信息的方法,而我想避免必须创建一个重载的方法,它接受字符串值而不是RemoteFileDP remoteFile。

The reason I need remoteFile to accept a string value is because there are MANY methods that utilize the information in remoteFile and I would like to avoid having to create a WHOLE BUNCH of overloaded methods that accept a string value instead of RemoteFileDP remoteFile.

因此,如果我可以接受这样的字符串值:

So if I can take a string value like:

string locationDirectory;

从另一种方法传入,然后具有类似于以下内容:

which is passed in from another method and then have something similar to the following:

RemoteFileDP remoteFile = locationDirectory;

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

then all other methods using remoteFile will not have to be overloaded or changed.

对不起,所有细节,但这是我第一次发布,希望我能提供足够的信息。我确实查看了 C#将动态字符串转换为现有Class C#:使用运行时确定的类型实例化对象,然后编写了以下代码:

Sorry for all the detail but this is my first time posting so I hope I provided enough information. I did look at C# Convert dynamic string to existing Class and C#: Instantiate an object with a runtime-determined type and wrote the following code:

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

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

However I keep getting a "MissingMethodException" error that the constructor for RemoteFileDP is not found but I do have the constructor as seen below:

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

谢谢您的协助!

推荐答案

您缺少使用字符串作为参数的构造函数。尝试使用

You're missing a constructor that takes a string as a parameter. Try your code with

public RemoteFileDP(string locationDirectory)
{
    // do stuff with locationDirectory to initialize RemoteFileDP appropriately
}

当然,如果这样做,为什么不直接调用

Of course, if you do that, why not just call the constructor directly?

RemoteFileDP remoteFile = new RemoteFileDP(locationDirectory);

这篇关于C#-将字符串转换为类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆