# Clr Interop and private member variables

**URL:** <https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170>\
**Category:** IronRuby\
**Created:** [May 5, 2009, 1:32am UTC](https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170 "2009-05-05T01:32:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [May 5, 2009, 1:32am UTC](https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170/1 "2009-05-05T01:32:43Z")

</div>

I have the following C# class  
namespace MyNamespace  
{  
public class MyObject  
{  
private string m\_Name;  
}  
}

and I’m extending it from IronRuby

class MyNameSpace::MyObject  
def printme  
puts m\_Name  
end  
end

When I try to run this, I get undefined method `m\_Name’ for  
MyNameSpace::MyObject (NoMethodError)  
Other normal method/property calls work fine.

Likewise if I do MyObject.new.instance\_eval{ m\_Name } the same error  
occurs.

Is this because IronRuby is trying to call a method instead of read a  
member  
variable, or is it because the variable is private?  
Either way, is there a way I can fix this? (short of falling back  
to InvokeMember…)

Thanks, Orion.

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [May 5, 2009, 1:42am UTC](https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170/2 "2009-05-05T01:42:05Z")

</div>

Make it protected instead of private?

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Orion E.  
Sent: Monday, May 04, 2009 4:32 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Clr Interop and private member variables

I have the following C# class

namespace MyNamespace  
{  
public class MyObject  
{  
private string m\_Name;  
}  
}

and I’m extending it from IronRuby

class MyNameSpace::MyObject  
def printme  
puts m\_Name  
end  
end

When I try to run this, I get undefined method `m\_Name’ for  
MyNameSpace::MyObject (NoMethodError)  
Other normal method/property calls work fine.

Likewise if I do MyObject.new.instance\_eval{ m\_Name } the same error  
occurs.

Is this because IronRuby is trying to call a method instead of read a  
member variable, or is it because the variable is private?  
Either way, is there a way I can fix this? (short of falling back to  
InvokeMember…)

Thanks, Orion.

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [May 5, 2009, 2:49am UTC](https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170/3 "2009-05-05T02:49:20Z")

</div>

Yes, that’s right. As Jim said, you can use -X:PrivateBinding to work  
around this, but I don’t remember if this has any other consequences.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Orion E.  
Sent: Monday, May 04, 2009 4:57 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Clr Interop and private member variables

It’s not my code 🙂

It appears as though “extending” a class in ruby is more or less the  
same as creating a derived class… is this right?

Might have to convince the other guy that “restricted by default” is not  
the best philosophy…  
On Tue, May 5, 2009 at 11:34 AM, Curt H.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

Make it protected instead of private?

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [May 5, 2009, 3:01am UTC](https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170/4 "2009-05-05T03:01:01Z")

</div>

PrivateBinding doesn’t mostly work in IronRuby yet (we will fix that).  
Note that the call to a private method/reading a private field will be  
much slower than if it was public. Also, it won’t work in partial trust  
(anywhere where your app is not granted full access to all .NET  
features, like in Silverlight, [ASP.NET](http://ASP.NET), etc.). I strongly suggest not to  
access private members unless you are writing unit tests for C# code.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Curt  
Hagenlocher  
Sent: Monday, May 04, 2009 5:47 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Clr Interop and private member variables

Yes, that’s right. As Jim said, you can use -X:PrivateBinding to work  
around this, but I don’t remember if this has any other consequences.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Orion E.  
Sent: Monday, May 04, 2009 4:57 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Clr Interop and private member variables

It’s not my code 🙂

It appears as though “extending” a class in ruby is more or less the  
same as creating a derived class… is this right?

Might have to convince the other guy that “restricted by default” is not  
the best philosophy…  
On Tue, May 5, 2009 at 11:34 AM, Curt H.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

Make it protected instead of private?

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [May 5, 2009, 1:57am UTC](https://www.ruby-forum.com/t/clr-interop-and-private-member-variables/167170/5 "2009-05-05T01:57:19Z")

</div>

It’s not my code 🙂  
It appears as though “extending” a class in ruby is more or less the  
same as  
creating a derived class… is this right?

Might have to convince the other guy that “restricted by default” is not  
the  
best philosophy…
