How do you use IronRuby to print a report of public methods and public properties of a C# class?

StackOverflow https://stackoverflow.com/questions/4992672

  •  13-11-2019
  •  | 
  •  

Question

Does anyone have a code sample showing how you reflect upon a random c# class from an arbitrary .NET assembly using IronRuby? I am most interested in listing the public methods and properties from a random C# class and .NET assembly.

I have tried to load "System.Reflection" from IronRuby. I can't seem to find a good code sample for doing this.

I know that Ruby can explore the meta data related to a class as well. I, however, can't seem to load my test assembly. (i.e. Test.dll)

Any help that you can offer would be helpful.

Was it helpful?

Solution

In order to load your Test.dll you will just need to require 'Test' assuming its visible to your script.

You can use the built-in Ruby method public_instance_methods but this will include those from Ruby's Object class, e.g.:

System::String.public_instance_methods

If you are only interested in the methods of your CLR type then you can access the Type of a given object through the IronRuby to_clr_type method. This gives you access to all the reflection methods that System.Type offers. For example, the following script will list all the public instance methods of a class Test.MyClass in Test.dll:

$: << 'path/to/dll'

require 'Test'

puts Test::MyClass.to_clr_type.get_methods(
  System::Reflection::BindingFlags.Public | System::Reflection::BindingFlags.Instance | System::Reflection::BindingFlags.DeclaredOnly)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top