Add method for getting type of var (#5070)

Co-authored-by: amylizzle <amylizzle@users.noreply.github.com>
This commit is contained in:
Amy
2024-05-06 20:39:32 +02:00
committed by GitHub
co-authored by amylizzle
parent 970da5f717
commit ccbb6ddec7
2 changed files with 23 additions and 0 deletions
@@ -264,6 +264,26 @@ namespace Robust.Shared.Serialization.Manager
return dataDefinition != null;
}
public bool TryGetVariableType(Type type, string variableName, [NotNullWhen(true)] out Type? variableType)
{
if (!TryGetDefinition(type, out var definition))
{
variableType = null;
return false;
}
var foundFieldDef = definition.BaseFieldDefinitions.FirstOrDefault(fieldDef => fieldDef?.BackingField.Name==variableName, null);
if(foundFieldDef != null)
{
variableType = foundFieldDef.BackingField.FieldType;
return true;
}
else
{
variableType = null;
return false;
}
}
private Type ResolveConcreteType(Type baseType, string typeName)
{
var type = ReflectionManager.YamlTypeTagLookup(baseType, typeName);