C# 将VB转换为C“GetCustomAttribute”

C# 将VB转换为C“GetCustomAttribute”,c#,vb.net,C#,Vb.net,我想用C语言翻译这个函数 Private Shared Sub GetTypes(ByVal AssemblyName As String) Dim ass As Assembly = Assembly.Load(AssemblyName) For Each value As Type In ass.GetTypes() Dim methods() As MethodInfo = value.GetMethods()

我想用C语言翻译这个函数

Private Shared Sub GetTypes(ByVal AssemblyName As String)
        Dim ass As Assembly = Assembly.Load(AssemblyName)
        For Each value As Type In ass.GetTypes()
            Dim methods() As MethodInfo = value.GetMethods()
            Dim InstanceType As MessageHandler
            For Each method As MethodInfo In methods
                [color=#40BF00]InstanceType = System.Attribute.GetCustomAttribute(method, GetType(MessageHandler), False)[/color]
                If InstanceType Is Nothing Then
                    Continue For
                End If
                Dim pack As DofusNetworkMessage = DirectCast(Activator.CreateInstance(InstanceType.MessageType), DofusNetworkMessage)
                Dim instance As InstanceInfo = New InstanceInfo(pack.ProtocolID, InstanceType.MessageType, method)
                Func.Add(New KeyValuePair(Of Integer, InstanceInfo)(pack.ProtocolID, instance))
            Next
        Next
    End Sub
我得到了这个

private static void InitializeFrames()
    {
        Assembly assembly = Assembly.Load("SmartBot.Engine");
        foreach (Type type in assembly.GetTypes())
        {
            MessageHandler InstanceType = null;

            foreach (MethodInfo Method in type.GetMethods())
            {
                [color=#FF0000]InstanceType = System.Attribute.GetCustomAttribute(Method, typeof(MessageHandler), false);[/color]
                if (InstanceType == null)
                {
                    continue;
                }
                NetworkMessage pack = (NetworkMessage)Activator.CreateInstance(InstanceType.MessageType);
                InstanceInfo instance = new InstanceInfo(pack.ProtocolId, InstanceType.MessageType, Method);
                Handles.Add(new KeyValuePair<int, InstanceInfo>(pack.ProtocolId, instance));
            }

        }
    }
我得到这个错误; 无法隐式转换类型“System”。属性“在”SmartBot中。发动机框架MessageHandler’。存在显式转换,是否缺少强制转换

你能帮我吗?

你只需向MessageHandler转换:


错误消息的这一部分提供了线索:显式转换存在,是否缺少强制转换?

异常消息告诉您无法将属性隐式转换为MessageHandler。函数返回一个属性-您需要显式转换为MessageHandler:

InstanceType = System.Attribute.GetCustomAttribute(Method, typeof(MessageHandler), false);
InstanceType = (MessageHandler)System.Attribute.GetCustomAttribute(Method, typeof(MessageHandler), false);
InstanceType = (MessageHandler)System.Attribute.GetCustomAttribute(Method, typeof(MessageHandler), false);