Duplicate Powershell Get-Members in C#

165 views Asked by At

PowerShell has the capability to enumerate the Methods and Properties of an object given a CLSID GUID via the Get-Members cmdlet. For example:

$test = [activator]::CreateInstance([type]::GetTypeFromCLSID("2735412C-7F64-5B0F-8F00-5D77AFBE261E"))
$test | Get-Member

This gives the following output:PowerShell Output

Using the same methods within C# gives a different results. I am using the following code to enumerate the objects Methods, Properties, and members:

Guid key = Guid.Parse("2735412C-7F64-5B0F-8F00-5D77AFBE261E");
Object instance = Activator.CreateInstance(Type.GetTypeFromCLSID(key));

MethodInfo[] methods = instance.GetType().GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.NonPublic);
PropertyInfo[] props = instance.GetType().GetProperties(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
ConstructorInfo[] consts = instance.GetType().GetConstructors(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
MemberInfo[] members = instance.GetType().GetMembers(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);

foreach (MemberInfo member in members)
{
    Console.WriteLine(member.Name);
}

This gives be the following output: C# Ouput

Any Ideas on why it's such a significant difference. The PowerShell output is great but, I need to do the same in C#...

EDIT 1: Remove the BindingFlags.* from the Get methods and the output looks like this C# Without BindingFlags

0

There are 0 answers