[Ironruby-core] Getting property list from both Ruby and .Net objects

Alexander Ranger lists at ruby-forum.com
Mon Sep 24 08:21:25 UTC 2012


I need some tools to get property names of both Ruby objects and .Net
objects. First I thought that there'll be no problem with it. I tried to
do it like:

def GetPropertyList(obj)
  return obj.instance_variables

But it turned out that when "obj" is a .Net object
obj.instance_variables returns nothing. Well, it returns an array but
array is empty.

For example:

require "Foo"
include FooModule

foo = Foo.new
foo.NetVal1 = "Hello"
foo.NetVal2 = 100

puts foo.class # => FooModule::Foo
puts foo.NetVal1 # => Hello
puts doo.NetVal2 # => 100

puts foo.instance_variables # =>             #it's blank here
puts foo.instance_variables.class # => Array

Well, then I thought that I can use reflection to get property list for
.Net objects:

def GetPropertyList(obj)
  array = Array.new
  obj.GetType().GetProperties.each do |prop|
    array.push prop.name

But it didn't work for a ruby object. I tried to somehow find out
weather object is from ruby or .Net and depending on it parse objects,
but I couldn't think of any working solution.

So my question is:
Give me a hint on how I can parse both ruby and .net objects using one

Thanks in advance,

Posted via http://www.ruby-forum.com/.

More information about the Ironruby-core mailing list