Is it possible to use Cecil to replace a field with a property? Specifically:
// Replace this.
public static readonly Something Thing = new Something(...);
// With this.
private static readonly Something _Thing = new Something(...);
public static Something Thing
{
get
{
// My goal is to insert some extra code here, which I can't do if it's a field.
return _Thing;
}
}
And if it is possible, can it be done efficiently? IE do I then need to go through every method in every type in every referencing assembly to replace every instance of Thing
with a call to the property's get_Thing
?
It would be a lot easier if I could just have the user write public static Something Thing { get; } = new Something(...);
public static Something Thing { get; } = new Something(...);
so it's already a property, but I can't because Unity's compiler doesn't support property initialisers.
Note: I know next to nothing about working with IL.
Yes, using Cecil you can easily remove a field and add a Property with the same name, but that'll change your API surface and you'll have to look for all the
IL_XXXX: ldsfld class [MyAssembly]Something [MyAssembly]MyClass::Thing
by
IL_XXXX: call class [MyAssembly]Something class [MyAssembly]MyClass::get_Thing()
and doing that in the current assembly and all the assemblies referencing it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.