# Getting property list from both Ruby and .Net objects

**URL:** <https://www.ruby-forum.com/t/getting-property-list-from-both-ruby-and-net-objects/223154>\
**Category:** IronRuby\
**Created:** [September 24, 2012, 10:21am UTC](https://www.ruby-forum.com/t/getting-property-list-from-both-ruby-and-net-objects/223154 "2012-09-24T10:21:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 24, 2012, 10:21am UTC](https://www.ruby-forum.com/t/getting-property-list-from-both-ruby-and-net-objects/223154/1 "2012-09-24T10:21:25Z")

</div>

Hello.

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  
end

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  
end  
array  
end

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  
algorithm.

Thanks in advance,  
Alex.

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [September 27, 2012, 8:12pm UTC](https://www.ruby-forum.com/t/getting-property-list-from-both-ruby-and-net-objects/223154/2 "2012-09-27T20:12:12Z")

</div>

Hi Alex:

Try:

obj.methods  
obj.public\_methods  
obj.private\_methods

it will give you the list of all the methods available

(From the point of view of IronRuby, a .net object shows the methods

you would find

two methods for, one for set another for get of a variable,

for example, saying that you have a propety of the object called “name”

obj.name (would return the actual setting for the variable (get) )

obj.name = “test”.to\_clr\_string (would set the name in the .net object)

Note:  
.to\_clr\_string is important to convert the ruby string object to the  
corresponding clr object (System::String)

Regards,

Eduardo

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [September 27, 2012, 8:17pm UTC](https://www.ruby-forum.com/t/getting-property-list-from-both-ruby-and-net-objects/223154/3 "2012-09-27T20:17:54Z")

</div>

Alex:  
A quick way of finding all the properties that you can set of a .net  
object would be:

obj.methods.grep(/=/)

(filter by all the properties that you can set)
