Invaderleige
Invaderleige

Reputation: 53

C# - Convert String to Class Object

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);

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();

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).

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).

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;

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

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);

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

Thank you ahead of time for your assistance!

Upvotes: 5

Views: 9750

Answers (3)

InBetween
InBetween

Reputation: 32780

Although I like the idea of a constructor accepting a string more, you could define an implicit or explicit conversion operator between RemoteFileDP and 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);
      }
 }

This way you could actually write:

 RemoteFileDP remoteFile = locationDirectory;

or, if the conversion operator were to be explicit:

 RemoteFileDP remoteFile = (RemoteFileDP)locationDirectory;

Still I insist, Anna Lear's solution is better as implicit or explicit conversion doesn't really seem to be the best fit for this kind of case. For instance, if the conversion can fail due to an invalid locationDictionary value then I wouldn't recommend this path. If the conversion is always succesful no matter what value locationDictionary is (barring null) then it could be a valid solution to your problem.

I'm just putting it on the table as I think you might find it useful to know about explicit and implicit conversions in C#, in case you didn't already.

Upvotes: 2

Thebigcheeze
Thebigcheeze

Reputation: 3498

If you don't wish to modify the source project that RemoteFileDP lives in (or can't) you could write an extension method such as below:

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

That way you could run the line of code you want:

RemoteFileDP remoteFile = locationDirectory;

With a slight modification as follows:

RemoteFileDP remoteFile = locationDirectory.ConvertToRemoteFileDP();

Would this allow you to solve your problem?

Upvotes: 2

Adam Lear
Adam Lear

Reputation: 38778

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);

Upvotes: 1

Related Questions