1

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

Adam Smith
  • 21
  • 2
  • They don't look like the same thing to me. If you remove all the binding flags can you at least see any of the public methods shown in the Powershell output? `CancelWrite` and `WriteSection`, for example, aren't even included in the C# list of methods. It looks like a list of purely inherited methods etc.. – Reinstate Monica Cellio Jun 11 '19 at 18:10
  • Right, they're not the same which, is odd... No, if you remove the binding flags, we get a different output. I have added an image above. – Adam Smith Jun 11 '19 at 18:14
  • I suspect that you're getting a default ComObject from `GetTypeFromCLSID()` in C#. Don't know why that would be, but it's what it looks like. – Reinstate Monica Cellio Jun 11 '19 at 19:16

0 Answers0