.net - propertyInfo.PropertyType is not assignable to its interface but the same type is - Stack Overflow

I am trying to ignore properties from a Swagger API if their type implements an interface.Here is my s

I am trying to ignore properties from a Swagger API if their type implements an interface.

Here is my schema filter:

Public Class cSwaggerIgnoreNavigationProperties
    Implements ISchemaFilter

    Public Sub Apply(voSchema As OpenApiSchema, voContext As SchemaFilterContext) Implements ISchemaFilter.Apply
        If voSchema.Properties.Count = 0 Then
            Return
        End If

        Const oFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public

        Dim oPropertyList = voContext.Type.GetProperties(oFlags)

        Dim oExcludedList =
            oPropertyList.Where(Function(p) p.PropertyType.IsAssignableTo(GetType(IEntity))) _
                .Select(Function(p)
                            Dim oCamelCaser As cCamelCaser = New cCamelCaser()
                            Return If(p.GetCustomAttribute(Of JsonPropertyNameAttribute)()?.Name, oCamelCaser.ToLowerCamelCase(p.Name))
                        End Function)

        For Each sExcluded In oExcludedList
            If voSchema.Properties.ContainsKey(sExcluded) Then
                voSchema.Properties.Remove(sExcluded)
            End If
        Next
    End Sub
End Class

This works in most cases, but misses certain types altogether. Whilst debugging, I found that p.PropertyType.IsAssignableTo(GetType(IEntity)) returns false unexpectedly for (at least) one of my types.

My Patient type in my S4D_Data assembly inherits from a class which implements IEntity.

Here are some outputs from the immediate window when breaking on that call:

?p.PropertyType.FullName
"S4D_Data.Patient"
?p.PropertyType.IsAssignableTo(GetType(S4D_Data.IEntity))
False

This shows that S4D_Data.Patient does definitely implement IEntity:

?GetType(S4D_Data.Patient).IsAssignableTo(GetType(S4D_Data.IEntity))
True

But this instance of propertyInfo.PropertyType even returns false when checking if it is its own type:

?p.PropertyType.IsAssignableTo(GetType(S4D_Data.Patient))
False
?p.PropertyType = GetType(S4D_Data.Patient)
False

Update

It looks like the issue may be related to having two instances of the S4D_Data assembly loaded, for some reason.

If I check the assembly for the named type and then for the reflected property type I get this output:

?GetType(S4D_Data.Patient).AssemblyQualifiedName
"S4D_Data.Patient, S4D Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
?p.PropertyType.AssemblyQualifiedName
"S4D_Data.Patient, V2e56f2547b0f45d2a33a0d70b908a55e.DynamicModels, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"

The reflected instance appears to reference a dynamic assembly.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744968432a4603823.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信