-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup environment project #518
Cleanup environment project #518
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love the new syntactic sugar.
{ | ||
if (sector.FDIndex != 0) | ||
if (control.Entries[sector.FDIndex].Find(e => e is FDTriggerEntry) is FDTriggerEntry trigger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know about you, but this variant is less readable to me than the original; if it's normal in C# world or the IDE complains that it should be coded this way, let's leave it as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's used like this in a few places across the solution, and there was a message about it here.
EMType.NOOP => JsonConvert.DeserializeObject<EMPlaceholderFunction>(jo.ToString(), this), | ||
|
||
_ => throw new InvalidOperationException(), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be coded in a more beautiful way, with the switch only mapping T
instead of the entire result?
Something such as this
EMType type = …;
Type resultType = type switch {
EMType.MirrorModel => EMImportModelFunction,
…,
};
return (
typeof(JsonConvert)
.GetMethod("DeserializeObject")
.MakeGenericMethod(resultType)
.Invoke(jo.ToString(), this)
);
or something similar? (Maybe there's a compile-time friendly alternative to GetMethod
in 2023, too)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Newtonsoft method is as follows, but there are several overloads.
public static T? DeserializeObject<T>(string value, params JsonConverter[] converters)
The following works, but it feels too dependent on reflection and difficult to read imo.
return typeof(JsonConvert)
.GetMethods()
.Where(m => m.Name == nameof(JsonConvert.DeserializeObject) && m.IsGenericMethod && m.GetParameters().Length == 2)
.First()
.MakeGenericMethod(emType)
.Invoke(null, new object[] { jo.ToString(), new JsonConverter[] { this } });
Another task we plan for is to move to System.Text.Json
(#506) and to be honest it is this environment deserialization that will be the sticking point. What do you think about re-addressing it there? I've done a little looking into it and the JObject
approach won't work out of the box anyway, so we're going to have to come up with a different parsing technique.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, if there are plans to migrate to something else anyway, there's no use to waste time on reworking this approach.
Cleanup tasks for the
TREnvironmentEditor
project.Part of #425.