dimanche 14 novembre 2021

Method GetCustomAttribute returns null, while on local debug it returns a value

I have a code that loads an assembly and categorize data by attributes.

I'm trying to get the actual CustomAttribute object. during debug locally the code returns a value (code compiles to .Net5 and net48)

but during runtime on a remote machine on (.Net5 target) the code returns null when i call GetCustomAttributes.

The Attribute:

[AttributeUsage(AttributeTargets.Interface)]
public class ApiInterfaceDescriptorAttribute : Attribute
{
    public string ApiUsageName { get; }

    public ApiInterfaceDescriptorAttribute(string apiUsageName)
    {
        ApiUsageName = apiUsageName;
    }
}

Sample interface

[ApiInterfaceDescriptor("powercontrol")]
public interface IMyInterface 
{
   [ApiMethodDescriptor(ApiExposureLevel.Basic, "a-method...")]
    void SomeMethod();
}

The trial to get the attribute

public class ApiDetector
{
    private Dictionary<Type, List<MethodInfo>> _apiDictionary = new Dictionary<Type, List<MethodInfo>>();
    public void LoadApiElements()
    {
        var apiKeyDesriptor = key.GetCustomAttribute(typeof(ApiInterfaceDescriptorAttribute)) as ApiInterfaceDescriptorAttribute;
            _apiDetector.Add(new ApiDataObjectDescriptor { Name = key.Name, ApiUsageName = apiKeyDesriptor?.ApiUsageName, Type = key });
    }

}

when i ran this code locally i get the instance: enter image description here

Running the code on .Net5 remote machine returns null:

enter image description here

Any help will be welcome. Thanks ahead!





Aucun commentaire:

Enregistrer un commentaire