# Calling a C# method with a byref parameter

**URL:** <https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309>\
**Category:** IronRuby\
**Created:** [February 11, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309 "2010-02-11T14:50:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 11, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/1 "2010-02-11T14:50:24Z")

</div>

Hi Everyone,

I’m trying to write a IronRuby script that interops with a a .NET  
assembly  
written in C#.

It has a class that derives from a base class in the .NET assembly. One  
of  
the base class protected methods looks like this:

protected void OnNotifyPropertyChanged(string name, ref T  
localmember, T  
value)  
{  
…  
}

I can’t for the life of me figure out how to call this method from IR in  
my  
derived class.  
The documentation mentions that you can call \*out \*parameters without  
using  
them as arguments, but I can’t seem to find anything about \*ref \*params.

If I just try calling the method like this:

OnNotifyPropertyChanged(property\_name, value, value)

I get this error:

_Microsoft.Scripting.Core:0:in `Bind’: Expression of type  
'IronRuby.Builtins.Muta_  
_bleString&’ cannot be used for parameter of type  
'IronRuby.Builtins.MutableStrin_  
_g’ of method 'Void  
 #base#OnNotifyPropertyChanged[MutableString](System.String, I_  
_ronRuby.Builtins.MutableString ByRef, IronRuby.Builtins.MutableString)’  
(Argumen_  
_tError)_

- 

```
   from Microsoft.Scripting.Core:0:in `BindCore'*

```

- 

```
   from ./dynamic_event_item.rb:22:in `method_missing'*

```

- 

```
   from :0*

```

Am I missing something?

Thanks

Michael E.

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 11, 2010, 3:27pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/2 "2010-02-11T15:27:53Z")

</div>

It returns another return value with the new ref value.

For example, if you have the next c# class:  
public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
    return "return value";
}

```

}

This is the IronRuby code you can write to use it:

> > > require ‘c:\dev\TestApps\TestClass\TestClass\bin\Debug\TestClass.dll’  
> > > =\> true  
> > > c = Class1.new  
> > > =\> TestClass.Class1  
> > > str = “hello”  
> > > =\> “hello”  
> > > result = c.do\_something(str)  
> > > =\> [‘return value’, ‘hellotamtamtam’]

Pay attention that the result variable is an array that contains the  
method  
return value as the first item and the ref value as the second  
parameter.

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
|  
Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)

On Thu, Feb 11, 2010 at 3:44 PM, Michael E.

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 11, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/3 "2010-02-11T15:45:27Z")

</div>

Thanks Shay.

OK your code works. But my trouble seems to come in with the Generic  
method.

When I do:

public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
        return "return value";
    }

    public string DoSomethingElse<T>(ref T test)
    {
        return test.ToString();
    }
}

```

and then call it from IR:

> > > c = TestClass::Class1.new  
> > > =\> TestClass.Class1  
> > > c.do\_something(“Bla”)  
> > > =\> [‘return value’, ‘Blatamtamtam’]  
> > > c.do\_something\_else(“Bla”)  
> > > Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'System.String DoSomethingElse[MutableString](IronRuby.Builtins.Mut ableString ByRef)' (ArgumentError) from Microsoft.Scripting.Core:0:in `BindCore’  
> > > from :0

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 11, 2010, 4:02pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/4 "2010-02-11T16:02:25Z")

</div>

You should call generic methods a bit differently.

The next code works:  
ret\_val = c.method(:do\_something\_else).of(String).call(str)

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
|  
Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)

On Thu, Feb 11, 2010 at 4:44 PM, Michael E.

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 11, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/5 "2010-02-11T16:57:27Z")

</div>

That call actually should (usually) be able to infer the type. Please  
file a bug on codeplex that Generic Type inference isn’t working with  
ByRef types.

JD

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Shay F.  
Sent: Thursday, February 11, 2010 6:53 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Calling a C# method with a byref parameter

You should call generic methods a bit differently.

The next code works:  
ret\_val = c.method(:do\_something\_else).of(String).call(str)

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
| Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)

On Thu, Feb 11, 2010 at 4:44 PM, Michael E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Thanks Shay.

OK your code works. But my trouble seems to come in with the Generic  
method.

When I do:

public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
        return "return value";
    }

    public string DoSomethingElse<T>(ref T test)
    {
        return test.ToString();
    }
}

```

and then call it from IR:

> > > c = TestClass::Class1.new  
> > > =\> TestClass.Class1  
> > > c.do\_something(“Bla”)  
> > > =\> [‘return value’, ‘Blatamtamtam’]  
> > > c.do\_something\_else(“Bla”)  
> > > Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'System.String DoSomethingElse[MutableString](IronRuby.Builtins.Mut ableString ByRef)' (ArgumentError) from Microsoft.Scripting.Core:0:in `BindCore’  
> > > from :0

On Thu, Feb 11, 2010 at 4:12 PM, Shay F.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
It returns another return value with the new ref value.

For example, if you have the next c# class:  
public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
    return "return value";
}

```

}

This is the IronRuby code you can write to use it:

> > > require ‘c:\dev\TestApps\TestClass\TestClass\bin\Debug\TestClass.dll’  
> > > =\> true  
> > > c = Class1.new  
> > > =\> TestClass.Class1  
> > > str = “hello”  
> > > =\> “hello”  
> > > result = c.do\_something(str)  
> > > =\> [‘return value’, ‘hellotamtamtam’]

Pay attention that the result variable is an array that contains the  
method return value as the first item and the ref value as the second  
parameter.

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
| Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)

On Thu, Feb 11, 2010 at 3:44 PM, Michael E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Hi Everyone,

I’m trying to write a IronRuby script that interops with a a .NET  
assembly written in C#.

It has a class that derives from a base class in the .NET assembly. One  
of the base class protected methods looks like this:

protected void OnNotifyPropertyChanged(string name, ref T  
localmember, T value)  
{  
…  
}

I can’t for the life of me figure out how to call this method from IR in  
my derived class.  
The documentation mentions that you can call out parameters without  
using them as arguments, but I can’t seem to find anything about ref  
params.

If I just try calling the method like this:

OnNotifyPropertyChanged(property\_name, value, value)

I get this error:

Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'Void #base#OnNotifyPropertyChanged[MutableString](System.String, I ronRuby.Builtins.MutableString ByRef, IronRuby.Builtins.MutableString)' (Argumen tError) from Microsoft.Scripting.Core:0:in `BindCore’  
from ./dynamic\_event\_item.rb:22:in `method\_missing’  
from :0

Am I missing something?

Thanks

Michael E.

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 11, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/6 "2010-02-11T18:56:51Z")

</div>

On what types will IR be able to infer the types? Is that documented on  
the  
site and I just didn’t see it?

Ryan R.

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 12, 2010, 2:29pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/7 "2010-02-12T14:29:51Z")

</div>

Ok, thanks Jim.

I have filed a bug here:

[http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3934](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3934)

[http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3934](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3934)I also  
noticed that event when using the form

method(:name).Of(String).Call(param)

If you’re calling a protected method from a derived class like this  
you’ll  
get a ArgumentError.

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 12, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/8 "2010-02-12T18:12:37Z")

</div>

Generic type inference should work like it does in C#. Iâ€™ll get some  
more tests in there and get Jimmy to update the docs.

JD

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ryan R.  
Sent: Thursday, February 11, 2010 9:56 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Calling a C# method with a byref parameter

On what types will IR be able to infer the types? Is that documented on  
the site and I just didn’t see it?

Ryan R.

On Thu, Feb 11, 2010 at 9:55 AM, Jim D.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
That call actually should (usually) be able to infer the type. Please  
file a bug on codeplex that Generic Type inference isnâ€™t working with  
ByRef types.

JD

From:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Shay F.  
Sent: Thursday, February 11, 2010 6:53 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Calling a C# method with a byref parameter

You should call generic methods a bit differently.

The next code works:  
ret\_val = c.method(:do\_something\_else).of(String).call(str)

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
| Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)  
On Thu, Feb 11, 2010 at 4:44 PM, Michael E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Thanks Shay.

OK your code works. But my trouble seems to come in with the Generic  
method.

When I do:

public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
        return "return value";
    }

    public string DoSomethingElse<T>(ref T test)
    {
        return test.ToString();
    }
}

```

and then call it from IR:

> > > c = TestClass::Class1.new  
> > > =\> TestClass.Class1  
> > > c.do\_something(“Bla”)  
> > > =\> [‘return value’, ‘Blatamtamtam’]  
> > > c.do\_something\_else(“Bla”)  
> > > Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'System.String DoSomethingElse[MutableString](IronRuby.Builtins.Mut ableString ByRef)' (ArgumentError) from Microsoft.Scripting.Core:0:in `BindCore’  
> > > from :0

On Thu, Feb 11, 2010 at 4:12 PM, Shay F.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
It returns another return value with the new ref value.

For example, if you have the next c# class:  
public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
    return "return value";
}

```

}

This is the IronRuby code you can write to use it:

> > > require ‘c:\dev\TestApps\TestClass\TestClass\bin\Debug\TestClass.dll’  
> > > =\> true  
> > > c = Class1.new  
> > > =\> TestClass.Class1  
> > > str = “hello”  
> > > =\> “hello”  
> > > result = c.do\_something(str)  
> > > =\> [‘return value’, ‘hellotamtamtam’]

Pay attention that the result variable is an array that contains the  
method return value as the first item and the ref value as the second  
parameter.

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
| Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)  
On Thu, Feb 11, 2010 at 3:44 PM, Michael E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Hi Everyone,

I’m trying to write a IronRuby script that interops with a a .NET  
assembly written in C#.

It has a class that derives from a base class in the .NET assembly. One  
of the base class protected methods looks like this:

protected void OnNotifyPropertyChanged(string name, ref T  
localmember, T value)  
{  
…  
}

I can’t for the life of me figure out how to call this method from IR in  
my derived class.  
The documentation mentions that you can call out parameters without  
using them as arguments, but I can’t seem to find anything about ref  
params.

If I just try calling the method like this:

OnNotifyPropertyChanged(property\_name, value, value)

I get this error:

Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'Void #base#OnNotifyPropertyChanged[MutableString](System.String, I ronRuby.Builtins.MutableString ByRef, IronRuby.Builtins.MutableString)' (Argumen tError) from Microsoft.Scripting.Core:0:in `BindCore’  
from ./dynamic\_event\_item.rb:22:in `method\_missing’  
from :0

Am I missing something?

Thanks

Michael E.

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

---

<div class="post-metadata">

**Author:** ![magic6435](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@magic6435](https://www.ruby-forum.com/u/magic6435)\
**Post date:** [February 12, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/calling-a-c-method-with-a-byref-parameter/183309/9 "2010-02-12T21:24:36Z")

</div>

About documentation, the bottom of  
[IronPython.net / Documentation / .NET Integration](http://ironpython.net/documentation/dotnet/) has this placeholder ☹

```
Appendix - Rules for Type parameter inference while invoking generic 

```

methods  
TODO

Jim, Dino had added tests for IronPython, and you should be able to  
leverage those for IronRuby

[http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3395](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3395) also  
tracks the issues with generic inference.

* * *

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[removed\_email\_address@domain.invalid] on behalf of Jim D.  
[removed\_email\_address@domain.invalid]  
Sent: Friday, February 12, 2010 9:06 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Calling a C# method with a byref parameter

Generic type inference should work like it does in C#. Iâ€™ll get some  
more tests in there and get Jimmy to update the docs.

JD

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ryan R.  
Sent: Thursday, February 11, 2010 9:56 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Calling a C# method with a byref parameter

On what types will IR be able to infer the types? Is that documented on  
the site and I just didn’t see it?

Ryan R.

On Thu, Feb 11, 2010 at 9:55 AM, Jim D.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
That call actually should (usually) be able to infer the type. Please  
file a bug on codeplex that Generic Type inference isnâ€™t working with  
ByRef types.

JD

From:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Shay F.  
Sent: Thursday, February 11, 2010 6:53 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Calling a C# method with a byref parameter

You should call generic methods a bit differently.

The next code works:  
ret\_val = c.method(:do\_something\_else).of(String).call(str)

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
| Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)  
On Thu, Feb 11, 2010 at 4:44 PM, Michael E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Thanks Shay.

OK your code works. But my trouble seems to come in with the Generic  
method.

When I do:

public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
        return "return value";
    }

    public string DoSomethingElse<T>(ref T test)
    {
        return test.ToString();
    }
}

```

and then call it from IR:

> > > c = TestClass::Class1.new  
> > > =\> TestClass.Class1  
> > > c.do\_something(“Bla”)  
> > > =\> [‘return value’, ‘Blatamtamtam’]  
> > > c.do\_something\_else(“Bla”)  
> > > Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'System.String DoSomethingElse[MutableString](IronRuby.Builtins.Mut ableString ByRef)' (ArgumentError) from Microsoft.Scripting.Core:0:in `BindCore’  
> > > from :0

On Thu, Feb 11, 2010 at 4:12 PM, Shay F.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
It returns another return value with the new ref value.

For example, if you have the next c# class:  
public class Class1  
{  
public string DoSomething(ref string test)  
{  
test = test + “tamtamtam”;

```
    return "return value";
}

```

}

This is the IronRuby code you can write to use it:

> > > require ‘c:\dev\TestApps\TestClass\TestClass\bin\Debug\TestClass.dll’  
> > > =\> true  
> > > c = Class1.new  
> > > =\> TestClass.Class1  
> > > str = “hello”  
> > > =\> “hello”  
> > > result = c.do\_something(str)  
> > > =\> [‘return value’, ‘hellotamtamtam’]

Pay attention that the result variable is an array that contains the  
method return value as the first item and the ref value as the second  
parameter.

## Shay.

Shay F. | .NET Technologies Expert | Author of IronRuby Unleashed  
| Sela Technology Center  
Blog: [http://IronShay.com](http://IronShay.com) | Twitter: [http://twitter.com/ironshay](http://twitter.com/ironshay)  
On Thu, Feb 11, 2010 at 3:44 PM, Michael E.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Hi Everyone,

I’m trying to write a IronRuby script that interops with a a .NET  
assembly written in C#.

It has a class that derives from a base class in the .NET assembly. One  
of the base class protected methods looks like this:

protected void OnNotifyPropertyChanged(string name, ref T  
localmember, T value)  
{  
…  
}

I can’t for the life of me figure out how to call this method from IR in  
my derived class.  
The documentation mentions that you can call out parameters without  
using them as arguments, but I can’t seem to find anything about ref  
params.

If I just try calling the method like this:

OnNotifyPropertyChanged(property\_name, value, value)

I get this error:

Microsoft.Scripting.Core:0:in `Bind': Expression of type 'IronRuby.Builtins.Muta bleString&' cannot be used for parameter of type 'IronRuby.Builtins.MutableStrin g' of method 'Void #base#OnNotifyPropertyChanged[MutableString](System.String, I ronRuby.Builtins.MutableString ByRef, IronRuby.Builtins.MutableString)' (Argumen tError) from Microsoft.Scripting.Core:0:in `BindCore’  
from ./dynamic\_event\_item.rb:22:in `method\_missing’  
from :0

Am I missing something?

Thanks

Michael E.

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)
