Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle case where IL has a string set to an sbyte value. #1

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,10 @@ static ElementType GetConstantType (TypeReference constant_type, object constant

return ElementType.Class;
case ElementType.String:
return ElementType.String;
if (constant is string)
return ElementType.String;
else
return GetConstantType (constant.GetType ());
case ElementType.Object:
return GetConstantType (constant.GetType ());
case ElementType.Array:
Expand Down Expand Up @@ -2201,7 +2204,7 @@ SignatureWriter GetConstantSignature (ElementType type, object value)
signature.WriteInt32 (0);
break;
case ElementType.String:
signature.WriteConstantString ((string) value);
signature.WriteConstantString (value);
break;
default:
signature.WriteConstantPrimitive (value);
Expand Down Expand Up @@ -2904,10 +2907,13 @@ bool TryWriteElementType (TypeReference type)
return true;
}

public void WriteConstantString (string value)
public void WriteConstantString (object value)
{
if (value != null)
WriteBytes (Encoding.Unicode.GetBytes (value));
if (value is string v)
WriteBytes (Encoding.Unicode.GetBytes (v));
else
WritePrimitiveValue (value);
else
WriteByte (0xff);
}
Expand Down
6 changes: 6 additions & 0 deletions Test/Mono.Cecil.Tests/FieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ public void ConstantCoalescing ()
Assert.IsTrue (field.HasConstant);
Assert.IsInstanceOf (typeof (char), field.Constant);
Assert.AreEqual ('s', field.Constant);

field = fields.GetField ("string_sbyte");
Assert.AreEqual ("System.String", field.FieldType.FullName);
Assert.IsTrue (field.HasConstant);
Assert.IsInstanceOf (typeof (sbyte), field.Constant);
Assert.AreEqual ((sbyte)0x20, field.Constant);
});
}

Expand Down
1 change: 1 addition & 0 deletions Test/Resources/il/types.il
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
.field private static literal char char_int16 = int16(0x0001)
.field private static literal int16 int16_char = char(0x0073)
.field private static literal int32 int32_nullref = nullref
.field private static literal string string_sbyte = int8(0x20)
}