mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
28 lines
753 B
C#
28 lines
753 B
C#
using System;
|
|
using System.Reflection;
|
|
|
|
namespace ZoneCodeGeneratorTests
|
|
{
|
|
internal static class FieldInjector
|
|
{
|
|
public static bool InjectToField(object target, object toInject)
|
|
{
|
|
if(target == null || toInject == null)
|
|
throw new ArgumentException("Params must not be null");
|
|
|
|
var injectType = toInject.GetType();
|
|
var fields = target.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
foreach(var field in fields)
|
|
{
|
|
if (!field.FieldType.IsAssignableFrom(injectType)) continue;
|
|
|
|
field.SetValue(target, toInject);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|