I have a class with a single method that uses a "yield" return statement. A nested type is automatically created. Using reflection with binding flags set to BindingFlags.DeclaredOnly
, I get this output:
// Public members from my class.
Test.FileSystemObject..ctor
Test.FileSystemObject.GetFiles(DirectoryInfo directory)
Test.FileSystemObject.GetFiles(String path)// Auto generated nested class. Test.FileSystemObject+<GetFiles>d__4..ctor Test.FileSystemObject+<GetFiles>d__4.<>3__directory Test.FileSystemObject+<GetFiles>d__4.<>4__this Test.FileSystemObject+<GetFiles>d__4.<directories>5__7 Test.FileSystemObject+<GetFiles>d__4.<files>5__8 Test.FileSystemObject+<GetFiles>d__4.<FSO>5__6 Test.FileSystemObject+<GetFiles>d__4.<i>5__9 Test.FileSystemObject+<GetFiles>d__4.<unprocessed>5__5 Test.FileSystemObject+<GetFiles>d__4.directory
How can I determine whether a type returned by assembly.GetTypes(BindingsFlags)
is such an auto generated type? I'm looking for a simple way to exclude these.
You can test if the type has the [CompilerGenerated]
attribute:
if (type.GetCustomAttribute(typeof(CompilerGeneratedAttribute), true) != null)
{
...
}
Alternatively, you can check if the name contains characters that wouldn't be valid in user code.