Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Convert String to Class Object
    text
    copied!<p>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:</p> <pre><code>RemoteFileDP remoteFile = new DPFactory().CreateRemoteFileDP(configData); </code></pre> <p>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:</p> <pre><code>ConfigDP configData = new ConfigDP(); </code></pre> <p>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).</p> <p>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). </p> <p>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.</p> <p>So if I can take a string value like: </p> <pre><code>string locationDirectory; </code></pre> <p>which is passed in from another method and then have something similar to the following:</p> <pre><code>RemoteFileDP remoteFile = locationDirectory; </code></pre> <p>then all other methods using remoteFile will not have to be overloaded or changed. </p> <p>Sorry for all the detail but this is my first time posting so I hope I provided enough information. I did look at <a href="https://stackoverflow.com/questions/4876683/c-convert-dynamic-string-to-existing-class">C# Convert dynamic string to existing Class</a> and <a href="https://stackoverflow.com/questions/981330/c-instantiate-an-object-with-a-runtime-determined-type">C#: Instantiate an object with a runtime-determined type</a> and wrote the following code:</p> <pre><code>RemoteFilesDP remoteFile = (RemoteFileDP)Activator.CreateInstance(typeof(RemoteFileDP), locationDirectory); </code></pre> <p>However I keep getting a "MissingMethodException" error that the constructor for RemoteFileDP is not found but I do have the constructor as seen below:</p> <pre><code>public RemoteFileDP() { } //end of RemoteFilePlattsDP constructor </code></pre> <p>Thank you ahead of time for your assistance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload