C
C#2mo ago
aa55h

Mono.Cecil TypeReference modifies TypeDefinition

i have this code:
MemberReference member = instruction.Operand;
if (Mappings.GetType(member.DeclaringType.Name, DefaultNamespace) is TypeDefinitionEntry typeDefinition)
{
WriteIfVerbose($"Remapping reference to {member.Name}'s type {member.DeclaringType.Name} -> {typeDefinition[TargetNamespace]}");
member.DeclaringType.Name = typeDefinition.Names[TargetNamespace];
}
MemberReference member = instruction.Operand;
if (Mappings.GetType(member.DeclaringType.Name, DefaultNamespace) is TypeDefinitionEntry typeDefinition)
{
WriteIfVerbose($"Remapping reference to {member.Name}'s type {member.DeclaringType.Name} -> {typeDefinition[TargetNamespace]}");
member.DeclaringType.Name = typeDefinition.Names[TargetNamespace];
}
the problem is the second line in the if statement body, it seems like it also modifies the class the member belongs to, and not only the reference to it, any idea why?
5 Replies
aa55h
aa55hOP2mo ago
before the modification: > Assembly.MainModule.Types: {Mono.Cecil.TypeDefinition[5]} [0]: {<Module>} [1]: {Microsoft.CodeAnalysis.EmbeddedAttribute} [2]: {System.Runtime.CompilerServices.RefSafetyRulesAttribute} [3]: {Hello.Creator} [4]: {Hello.Program} after: {Mono.Cecil.TypeDefinition[5]} [0]: {<Module>} [1]: {Microsoft.CodeAnalysis.EmbeddedAttribute} [2]: {System.Runtime.CompilerServices.RefSafetyRulesAttribute} [3]: {Hello.NamedCreator} [4]: {Hello.Program} you see the NamedCreator
canton7
canton72mo ago
That makes sense to me -- Member.DeclaringType is the type which contains the member, and then you're changing its Name property What are you trying to do?
aa55h
aa55hOP2mo ago
But it returns TypeReference and not TypeDefinition, so i would expect to change only this reference to the class(the code it modifies is: Creator.DoSmth()) I thought it would only swap the Creator for NamedCreator and not rename the class itself
canton7
canton72mo ago
I don't know Mono.Cecil particularly well, but I would assume that you'd want to reassign member.DeclaringType, not member.DeclaringType.Name
aa55h
aa55hOP2mo ago
awesome, it seems it was fixed, thanks
Want results from more Discord servers?
Add your server