Skip to content

Instantly share code, notes, and snippets.

@gustavopsantos
Created February 7, 2023 13:07
Show Gist options
  • Save gustavopsantos/bef0d8400df272483fb8fdb05dc56278 to your computer and use it in GitHub Desktop.
Save gustavopsantos/bef0d8400df272483fb8fdb05dc56278 to your computer and use it in GitHub Desktop.
InjectedPropertyInfo
internal sealed class InjectedPropertyInfo
{
public readonly Type PropertyType;
public readonly Action<object, object> Setter;
public InjectedPropertyInfo(PropertyInfo propertyInfo)
{
PropertyType = propertyInfo.PropertyType;
Setter = GenerateSetter(propertyInfo);
}
private static Action<object, object> GenerateSetter(PropertyInfo propertyInfo)
{
#if ENABLE_MONO
var objParam = Expression.Parameter(typeof(object), "obj");
var convertedObj = Expression.Convert(objParam, propertyInfo.DeclaringType!);
var valueParam = Expression.Parameter(typeof(object), "value");
var convertedValue = Expression.Convert(valueParam, propertyInfo.PropertyType);
var propertyExpression = Expression.Property(convertedObj, propertyInfo);
var assignExpression = Expression.Assign(propertyExpression, convertedValue);
return Expression.Lambda<Action<object, object>>(assignExpression, objParam, valueParam).Compile();
#elif ENABLE_IL2CPP
return propertyInfo.SetValue;
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment