search loaded types for the IMessage interface before loading the Google.Protobuf DLL

also make lambdas static where possible
This commit is contained in:
Xpl0itR 2024-03-19 00:11:12 +00:00
parent c61334d291
commit 68ee943b9d
Signed by: Xpl0itR
GPG Key ID: 91798184109676AD
3 changed files with 9 additions and 8 deletions

View File

@ -35,7 +35,8 @@ public sealed class AssemblyInspector : IDisposable
public IEnumerable<Type> GetProtobufMessageTypes()
{
Type? googleProtobufIMessage = AssemblyContext.LoadFromAssemblyName("Google.Protobuf")
Type? googleProtobufIMessage = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Google.Protobuf.IMessage", null)
?? AssemblyContext.LoadFromAssemblyName("Google.Protobuf")
.GetType("Google.Protobuf.IMessage");
return from type
in LoadedTypes
@ -58,7 +59,7 @@ public sealed class AssemblyInspector : IDisposable
{
public readonly IReadOnlyDictionary<string, string> AssemblyPathLookup =
assemblyPaths.ToDictionary(
path => Path.GetFileNameWithoutExtension(path),
static path => Path.GetFileNameWithoutExtension(path),
StringComparer.OrdinalIgnoreCase);
/// <inheritdoc />

View File

@ -44,7 +44,7 @@ public sealed class Message : Protobuf
writer.WriteLine(" {");
writer.Indent++;
int[] oneOfs = OneOfs.SelectMany(oneOf => oneOf.Value).ToArray();
int[] oneOfs = OneOfs.SelectMany(static oneOf => oneOf.Value).ToArray();
foreach ((int fieldId, (bool, string, string) field) in Fields)
{

View File

@ -104,8 +104,8 @@ public sealed class ProtodecContext
{
string oneOfName = TranslateOneOfName(property.Name);
int[] oneOfProtoFieldIds = propertyType.GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(field => (int)field.GetRawConstantValue()!)
.Where(id => id > 0)
.Select(static field => (int)field.GetRawConstantValue()!)
.Where(static id => id > 0)
.ToArray();
message.OneOfs.Add(oneOfName, oneOfProtoFieldIds);
@ -242,7 +242,7 @@ public sealed class ProtodecContext
private string TranslateEnumFieldName(FieldInfo field, string enumName)
{
if (field.GetCustomAttributesData()
.SingleOrDefault(attr => attr.AttributeType.Name == "OriginalNameAttribute")
.SingleOrDefault(static attr => attr.AttributeType.Name == "OriginalNameAttribute")
?.ConstructorArguments[0]
.Value
is string originalName)
@ -280,6 +280,6 @@ public sealed class ProtodecContext
private static bool HasProtocAttribute(MemberInfo member) =>
member.GetCustomAttributesData()
.Any(attr => attr.AttributeType.Name == nameof(GeneratedCodeAttribute)
.Any(static attr => attr.AttributeType.Name == nameof(GeneratedCodeAttribute)
&& attr.ConstructorArguments[0].Value as string == "protoc");
}