C
C#4mo ago
Oombuu

Get Object of unknown type with unknown parameters from string

I have an abstract parent class with several child classes, each child class has an export method that returns a string that hold the data of the instance of the subclass in the form of: [SUBCLASS_NAME] | [PARAM#1] | [PARAM#2] | [PARAM#3]... and so on for all of the parameters, these parameters can be Enum values (The Enums are located in the parent class), strings, integers, and possibly in the future, doubles / floats. I want to write a static method in the parent class that will return an instance of the child class defined at the beginning of the input string, that will then use the remaining data in the string as parameters. Currently I lack the understanding of types as well as the GetConstructor method, and also the understanding to assemble the type list properly, I've read the official documentation but it was a little too complex to grasp, could somebody help me figure out how to achieve this? Attached is my current attempt.
No description
24 Replies
Anton
Anton4mo ago
tList.Add(…) you just need o.GetType() there you don't need the tryparse loop either ah right you have types as strings in slist you need to Type.GetType($"{parentClass}.{sList[i]") also they might be (and likely will be) in some namespace how do you want to handle that?
Oombuu
OombuuOP4mo ago
well, they're all in the same namespace as the parent class, so I didn't think that would be an issue
Anton
Anton4mo ago
I mean the parent type
Oombuu
OombuuOP4mo ago
oh... I didn't know factoring in the namespace was an issue
Anton
Anton4mo ago
does subclass name include the namespace? I don't think it will be able to find the type otherwise If they're not public, I think you need to pass binding flags
Oombuu
OombuuOP4mo ago
binding flags?
Anton
Anton4mo ago
an optional parameter ah no that's for properties
Oombuu
OombuuOP4mo ago
alright I'll update my code and get back to you to see if it works, thank you so much
Anton
Anton4mo ago
yw
Oombuu
OombuuOP4mo ago
I'm still not fully sure I understand my intent is to be able to take:
typeof(EftDamage) + "5|TickType.SpeedTick|TargetType.OneAlly"
typeof(EftDamage) + "5|TickType.SpeedTick|TargetType.OneAlly"
and plug it into the constructor
public EftDamage(int damage, TickType tickType = TickType.Default, TargetType targetType = TargetType.OneOpponent)
public EftDamage(int damage, TickType tickType = TickType.Default, TargetType targetType = TargetType.OneOpponent)
and have it return the EftDamage object with parameters filled in, while also having the same code be able to handle taking
typeof(EftDuration) + "8|TickType.UserEnd|foo"
typeof(EftDuration) + "8|TickType.UserEnd|foo"
and plug it into
public EftDuration(int duration, TickType tickType, string label)
public EftDuration(int duration, TickType tickType, string label)
an return the EftDuration object with its parameters filled in. All classes are in the same namespace and are children classes of the ActEffect class I'm writing this method in. I'n not sure if my current code is on the right path or not. (it may be worth noting that both the Enums TickType and TargetType are in the class that this is being written in)
Anton
Anton4mo ago
I see why is that a string again?
Oombuu
OombuuOP4mo ago
well, I suppose it doesnt have to be, String was the immediate data type I say that could represent all the other data types I'd need, and as an added bonus I can use it in the BinaryWriter to easily write these onjects to files (these are only going to be a small aprt of the actual file that is exported)
Anton
Anton4mo ago
what do you need this for? If it's for scripting or configuration, json might be better
Oombuu
OombuuOP4mo ago
well, I would specifically like to use the BinaryReader and BinaryWriter if possible, I want to make a custom file type when exporting and importing them its for a video game
Anton
Anton4mo ago
If it's going to be a custom format and you're going to remake this later, don't overcomplicate the format right now. Use json or xml. if you want a custo format like you wrote you need a couple of things you should write a parser and not cut any corners when it comes to parsing you want to type check both the parent type and the argument types in advance you probably want a registry for your effect types, so that the user can't instantiate just any type this way for security reasons Type.GetType should generally never be used
Oombuu
OombuuOP4mo ago
No description
Anton
Anton4mo ago
if you don't have a registry, you need to actually store the metadata name in the string
Oombuu
OombuuOP4mo ago
registry? does this count? sorry wrong ss
Oombuu
OombuuOP4mo ago
No description
Oombuu
OombuuOP4mo ago
this here is where I make a list of all the subclasses, of this class. It works
Anton
Anton4mo ago
yeah, search in this list when you look for the type not Type.GetType
Oombuu
OombuuOP4mo ago
alright, and should I just manually put in an array of types to each effect type that I call when interpreting the input string? for the parameters. or is there a more automatic way to get the type list from a constructor?
Anton
Anton4mo ago
make a class for a registry with this info pass that as a parameter to your method that does the instantiation you don't need it in the constructor you need it in the method that parses the serialized data
Oombuu
OombuuOP4mo ago
alright, I'll do my best. Thank you for your help and time
Want results from more Discord servers?
Add your server