Create a method to convert types from string to that type #15

Open
opened 6 years ago by SimonG96 · 0 comments
SimonG96 commented 6 years ago (Migrated from github.com)
Owner

Is there a need for a method that converts user types from string to that specific type?

If so, create an interface IGBaseObject that provides a method FromString() like this:

public interface IGBaseObject<T>
{
    public T FromString(string @string);
}

Another possibility would be to implement a static method FromString() that creates a new instance in every class that needs this:

public static T FromString(string @string)
{
    //Do stuff to be able to create a new T
    return new T();
}

Third we could add a constructor and/or a factory with only a string as parameter and create a new instance that way:

public T(string @string)
{
    //Do stuff to be able to create a new T
}
public interface ITFactory
{
    public IT Create(string @string);
}

Disadvantages of the last two solutions would be that a user would have to remember to implement these for every type, and you wouldn't be able to call it at a place where you don't have an instance of the actual type.

Is there a need for a method that converts user types from string to that specific type? If so, create an interface `IGBaseObject` that provides a method `FromString()` like this: ```c# public interface IGBaseObject<T> { public T FromString(string @string); } ``` Another possibility would be to implement a static method `FromString()` that creates a new instance in every class that needs this: ```c# public static T FromString(string @string) { //Do stuff to be able to create a new T return new T(); } ``` Third we could add a constructor and/or a factory with only a `string` as parameter and create a new instance that way: ```c# public T(string @string) { //Do stuff to be able to create a new T } ``` ```c# public interface ITFactory { public IT Create(string @string); } ``` **Disadvantages** of the last two solutions would be that a user would have to remember to implement these for every type, and you wouldn't be able to call it at a place where you don't have an instance of the actual type.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

No dependencies set.

Reference: SimonG/GBase#15
Loading…
There is no content yet.