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.
on 2012-09-24 10:21
on 2012-09-27 20:12
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
on 2012-09-27 20:17
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)
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.