[Mono-devel-list] Re: [mono-vb] VB Class status
Jambunathan Jambunathan
kjambunathan at novell.com
Thu Jun 24 04:12:31 EDT 2004
Rafael
> What makes that .param [n] appear there?
> [opt] comes from the Optional attribute? it seems so, but ...
( All these observations based on Windows XP box)
[opt] does get generated by [Optional]
.param does get generated by System.ComponentModel.DefaultValue.
Interestingly however, for the HasDefault flag to be turned on
the compiler apparently looks for ".param[] = " construct.
Not sure what would segment of C# code would force
emitting of ".param[] = " construct. I believe that there is no such
construct in C# though.
Anyways the functionality is not broken ( if you use vbc as opposed to mbas )
only that the MSVB class status page simply refuse to recognize all the
good effort that we have put in ...
Should corcompare be changed to accomodate this ? Shouldn't be a big deal
I think.
Regards,
Jambunathan K.
ps:
Just out of curisoity I did this:
Compiled the following SampleClass.cs
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Reflection;
class SampleClass
{
public static void Main()
{
Assembly thisAss = Assembly.GetExecutingAssembly();
Type type = thisAss.GetType("SampleClass");
MethodInfo method = type.GetMethod("SampleMethod");
ParameterInfo [] parameters = method.GetParameters();
foreach(ParameterInfo param in parameters)
{
Console.WriteLine("param name = {0}, param attribute = {1}",
param.Name, param.Attributes);
}
}
public static void SampleMethod(int a, [Optional][DefaultValue(10)] int b)
{
Console.WriteLine(b);
}
}
I see that parameter b has Optional bit turned on but not the HasDefault bit.
The IL code for the above exe looks like this.
.method public hidebysig static void SampleMethod(int32 a, [opt] int32 b) cil managed
{ .param [2] // notice that there is no "=something"
.custom instance void [System]System.ComponentModel.DefaultValueAttribute::.ctor(int32) = ( 01 00 0A 00 00 00 00 00 )
// Code size 7 (0x7) .maxstack 1
IL_0000: ldarg.1
IL_0001: call
void [mscorlib]System.Console::WriteLine(int32)
IL_0006: ret
} // end of method SampleClass::SampleMethod
If in the above IL segment, I replace .param [2] with .param[2]=0x0000000A
and comment out .custom instance blah blah and compile the modified IL
back to an exe and execute the same I see that the parameter b has now
both Optional and HasDefault flags turned on.
>> "A Rafael D Teixeira" <rafaelteixeirabr at hotmail.com> 6/24/2004 1:13:54 AM >>>
Inline
>From: "Jambunathan Jambunathan" <kjambunathan at novell.com>
>
>The errors in Microsoft.VisualBasic may seem intimidating at first.
>Most of the errors are due to a mismatch in the Parameter Attributes.
>
>Many VB functions/methods support optional parameters with a default
>value. MS's dll marks the Parameter with Optional, HasDefault
>attributes.
>
>In our C# implementation of the method, we mark such parameters using
>the
>following two attributes: System.Runtime.InteropServices.Optional
>and System.ComponentModel.DefaultValue(). As a result Mono's dll has
>Parameter Attributes just set to Optional ( but has no HasDeafult ).
>
>I am not sure whether this is bug or a baggage not carried from the
>past.
>( Quick googling suggested that just using C# it's not possible to
>create a parameter with Parameter Attributes set to Optional |
>HasDeafult.
>
>Just posting to the mailing list with the hope that I will get a quick
>response.
It's indead trickier:
code like
Private Sub x(ByVal w As String, Optional ByVal y As Integer = 132)
TextBox1.Text = w & y
End Sub
Private Sub z()
x("what", 1)
x("whatnot")
End Sub
generates things like
.method private instance void x(string w,
[opt] int32 y) cil managed
{
.param [2] = int32(0x00000084)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
...
IL_001a: ret
} // end of method Form1::x
<comments>
we need to generate it properly for MS.VB.Dll but we still need more
information to know how. What makes that .param [n] appear there? [opt]
comes from the Optional attribute? it seems so, but ...
That is why I guessed DefaultValue, but it seems it is not the proper thing
to use...
Maybe our truly Jackson of mono´s ilasm fame can bear some light for us...
</comments>
and
.method private instance void z() cil managed
{
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldstr "what"
IL_0007: ldc.i4.1
IL_0008: callvirt instance void TesteXmlNText.Form1::x(string,
int32)
IL_000d: nop
IL_000e: ldarg.0
IL_000f: ldstr "whatnot"
IL_0014: ldc.i4 0x84
IL_0019: callvirt instance void TesteXmlNText.Form1::x(string,
int32)
IL_001e: nop
IL_001f: nop
IL_0020: ret
} // end of method Form1::z
<comments>
that mbas must take care to generate, see that the compiler discover the
default value and pass it
to the method, but it complicates even more when you add overload-resolution
to the figure, and also late-binding (we will have to implement all that
policies inside the helping classes in MS.VB.dll also).
</comments>
So indeed a long way to go,
Have fun boys,
Rafael "Monoman" Teixeira
Mono Hacker since 16 Jul 2001 - http://www.go-mono.org/
MonoBrasil Founding Member - Membro Fundador do MonoBrasil
http://monobrasil.softwarelivre.org
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/
_________________________________________________________________
MSN Messenger: instale grátis e converse com seus amigos.
http://messenger.msn.com.br
More information about the Mono-devel-list
mailing list