Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in Fields property of ContextualType #157

Open
vvdb-architecture opened this issue Jul 29, 2024 · 0 comments
Open

Exception in Fields property of ContextualType #157

vvdb-architecture opened this issue Jul 29, 2024 · 0 comments

Comments

@vvdb-architecture
Copy link

vvdb-architecture commented Jul 29, 2024

Consider the following definition:

        class Base
        {
            public int Field1;
        }

        class Derived<T> : Base
        {
            public T? Field2;
        }

... then the following code will fail when the Fields property is called:

            var ctype = typeof(Derived<int>).ToContextualType();
            foreach (var f in ctype.Fields)
            {
                Console.WriteLine(f.Name);
            }

The reason is explained in a frustrating NSwag bug reported here: RicoSuter/NSwag#4842 (comment)

This is using Namotion.Reflection 3.1.1.

After some further digging, it looks like in the master bransh of Namotion, this was corrected.
Merged pull request #151 (comment)

    _fields = Type.GetRuntimeFields().Select(field =>
                            {
                                if (field.DeclaringType is { IsGenericType: true })
                                {
                                    var genericType = field.DeclaringType.GetGenericTypeDefinition();
                                    var genericField = genericType.GetRuntimeField(field.Name);
                                    if (genericField != null && genericField.FieldType.IsGenericParameter)
                                    {
                                        var actualType = GenericArguments[genericField.FieldType.GenericParameterPosition];
                                        var actualIndex = actualType._nullableFlagsIndex;
                                        return new ContextualFieldInfo(field, ref actualIndex, actualType._nullableFlags);
                                    }
                                }

                                var index = 0;
                                return new ContextualFieldInfo(field, ref index, null);
                            }).ToArray();

... but I don't know how to proceed to generate a new version of NSwag with this included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant