Skip to content

Assembly.GetTypes fails on System.DirectoryServices assembly #68240

@gamingrobot

Description

@gamingrobot

Description

When calling Assembly.GetTypes on the assembly System.DirectoryServices a ReflectionTypeLoadException is thrown.

Unhandled exception. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load type 'System.DirectoryServices.ActiveDirectory.LSA_FOREST_TRUST_RECORD' from assembly 'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' because it contains an object field at offset 16 that is incorrectly aligned or overlapped by a non-object field.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.Assembly.GetTypes()
   at DirectoryServicesIssue.Program.Main(String[] args) in C:\Development\test\carebear\DirectoryServicesIssue\Program.cs:line 13
System.TypeLoadException: Could not load type 'System.DirectoryServices.ActiveDirectory.LSA_FOREST_TRUST_RECORD' from assembly 'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' because it contains an object field at offset 16 that is incorrectly aligned or overlapped by a non-object field.

Reproduction Steps

  1. Create new console app using .NET 7.0.0 Preview 3
  2. Add System.DirectoryServices package <PackageReference Include="System.DirectoryServices" Version="7.0.0-preview.3.22175.4" />
  3. Add the code below to Program.cs
  4. Run
//Use System.DirectoryServices
var _ = new DirectorySearcher("testing");
//GetTypes
var assembly = AppDomain.CurrentDomain.GetAssemblies().Single(x => x.GetName().Name == "System.DirectoryServices");
assembly.GetTypes();

Expected behavior

ReflectionTypeLoadException should not be thrown

Actual behavior

ReflectionTypeLoadException is thrown

Regression?

Yes, works as expected when using the 6.0.0 version of the System.DirectoryServices nuget package

Known Workarounds

Don't call GetTypes on the System.DirectoryServices assembly

Configuration

.NET 7.0.0 Preview 3
Windows 10 21H2
x64

Other information

I think it is due to this change here a61c1a3#diff-bb93fc33786f17ce709a25bd79b94cf4e6c5e2ba8ac5b4b0f728dcb35d2ac77dL415

LSA_UNICODE_STRING (class) was replaced with global::Interop.UNICODE_STRING (struct) causing a mismatch since LSA_FOREST_TRUST_BINARY_DATA and LSA_FOREST_TRUST_DOMAIN_INFO are both classes and use the same FieldOffset

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions