Created
February 7, 2023 13:07
-
-
Save gustavopsantos/bef0d8400df272483fb8fdb05dc56278 to your computer and use it in GitHub Desktop.
InjectedPropertyInfo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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