GetProto automation method for all types with ProtoContract attribute

I want to use reflection to look at all types in a .dll and generate a .proto file for everyone that has a ProtoContract attribute. I am going to use .proto files to create C ++ classes, so my C # code can interact with some C ++. This code will scan the DLL to find types with the ProtoContract attribute, but I don’t know how to dynamically pass this type to GetProto ().

//get assemblies in directory.
string file = @"C:\bungie\networking\shared\online\output\bin\Test\Xetrov\Xetrov.Core\Xetrov.Core.dll";
var assembly = Assembly.LoadFile(file);
foreach (var type in assembly.GetTypes())
{
    if (!type.IsClass || type.IsNotPublic) 
    {
        continue;
    }
    //Get all the attribute for the type
    object[] attributes = type.GetCustomAttributes(true);
    //Look for the ProtoContract attribute
    if(attributes.Where(att => att is ProtoBuf.ProtoContractAttribute).Any())
    {
        // We have a type that protobuf can use, generate the .proto file
            // How to tell it what type T is??  can't use variable type
        string protoDefinition = ProtoBuf.Serializer.GetProto<T>();
    }
}

Any ideas how I can do this? Or is the best way to generate .proto files for all types?

Thank!!

+3
source share
1 answer

-, , GetProto v2, , - . , v1. , , MakeGenericMethod - :

// outside loop
var method = typeof(ProtoBuf.Serializer).GetMethod("GetProto");
...
// inside loop
var proto = untyped.MakeGenericMethod(type).Invoke(null, null);

v2, - API (v2 ).

+1

All Articles