mysimulation/server/tso.common/Utils/DotPath.cs
Tony Bark 22191ce648 Removed NioTSO client and server
- NioTSO client isn't needed because we're using RayLib
- Added FreeSO's API server to handle most backend operations
2024-05-01 02:55:43 -04:00

54 lines
1.6 KiB
C#
Executable file

using System;
using System.Reflection;
namespace FSO.Common.Utils
{
public static class DotPath
{
public static PropertyInfo[] CompileDotPath(Type sourceType, string sourcePath)
{
//Dot path
var path = sourcePath.Split(new char[] { '.' });
var properties = new PropertyInfo[path.Length];
var currentType = sourceType;
for (int i = 0; i < path.Length; i++)
{
var property = currentType.GetProperty(path[i]);
properties[i] = property;
currentType = property.PropertyType;
}
return properties;
}
public static object GetDotPathValue(object source, PropertyInfo[] path)
{
if (source == null) { return null; }
var currentValue = source;
for (var i = 0; i < path.Length; i++)
{
currentValue = path[i].GetValue(currentValue, null);
if (currentValue == null) { return null; }
}
return currentValue;
}
public static void SetDotPathValue(object source, PropertyInfo[] path, object value)
{
if (source == null) { return; }
var currentValue = source;
for (var i = 0; i < path.Length - 1; i++)
{
currentValue = path[i].GetValue(currentValue, null);
if (currentValue == null) { return; }
}
var member = path[path.Length - 1];
member.SetValue(currentValue, value, null);
}
}
}