2011-05-07 13 views
9

में काम करते हैं, मैं clay in VB.Net को आजमा रहा था लेकिन पाया कि मुझे सबकुछ काम करने में नहीं मिला।वीबी.Net का उपयोग करते समय मेरे अज्ञात प्रकार क्ले में क्यों काम नहीं करते हैं, लेकिन सी #

यहाँ है Clay

इस में काम करता है सी #

dynamic c = new ClayFactory(); 
     var plant = c.Plant(new {LatinName = "test"}); 
     Console.WriteLine(plant.LatinName); 
     Console.ReadLine(); 

लेकिन इस VB.Net में काम नहीं करता

Dim c As Object = New ClayFactory 
     Dim plant = c.Plant(New With {.LatinName = "test"}) 
     Console.WriteLine(plant.LatinName) 
     Console.ReadLine() 

मैं VB.Net में इस त्रुटि संदेश मिलता है:

> Cannot close over byref parameter 
> '$arg1' referenced in lambda '' 

मैं 100% निश्चित नहीं हूं कि इसे हल करने के लिए कैसे करें यदि मैं इसे हल भी कर सकता हूं। मैं अनुमान लगा रहा हूं कि अनाम प्रकारों के वीबी.Net कार्यान्वयन थोड़ा अलग है।

मैं इस लाइन पर त्रुटि मिलती है:

Dim plant = c.Plant(New With {.LatinName = "test"}) 

मैं अगर किसी ने मुझे यह समझाने सराहना करेंगे।

आईएल अलग छोड़ दिया प्रतीत होता है।

वीबी के लिए निजी क्षेत्र Latinname यह है:

.field private initonly !T0 $LatinName 

सी # के लिए यह है:

.field private initonly !'<LatinName>j__TPar' '<LatinName>i__Field' 
.custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = (01 00 00 00 00 00 00 00) 

और सार्वजनिक विधि Get_LatinName यह है।

वीबी:

 
    .method public specialname instance !T0 get_LatinName() cil managed 
{ 
    .custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = (01 00 00 00) 
    // Code size  11 (0xb) 
    .maxstack 1 
    .locals init (!T0 V_0) 
    IL_0000: ldarg.0 
    IL_0001: ldfld  !0 class VB$AnonymousType_0`1::$LatinName 
    IL_0006: stloc.0 
    IL_0007: br.s  IL_0009 
    IL_0009: ldloc.0 
    IL_000a: ret 
} // end of method VB$AnonymousType_0`1::get_LatinName 

सी #:

 

    .method public hidebysig specialname instance !'j__TPar' 
     get_LatinName() cil managed 
{ 
    // Code size  11 (0xb) 
    .maxstack 1 
    .locals init (!'j__TPar' V_0) 
    IL_0000: ldarg.0 
    IL_0001: ldfld  !0 class 'f__AnonymousType0`1'j__TPar'>::'i__Field' 
    IL_0006: stloc.0 
    IL_0007: br.s  IL_0009 
    IL_0009: ldloc.0 
    IL_000a: ret 
} // end of method 'f__AnonymousType0`1'::get_LatinName 

और इन प्रमुख पद्धतियां हैं:

वीबी:

 
    .method public static void Main() cil managed 
{ 
    .entrypoint 
    .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = (01 00 00 00) 
    // Code size  90 (0x5a) 
    .maxstack 7 
    .locals init ([0] object c, 
      [1] object plant, 
      [2] class VB$AnonymousType_0`1 VB$t_ref$S0, 
      [3] object[] VB$t_array$S0) 
    IL_0000: nop 
    IL_0001: newobj  instance void [ClaySharp]ClaySharp.ClayFactory::.ctor() 
    IL_0006: stloc.0 
    IL_0007: ldloc.0 
    IL_0008: ldnull 
    IL_0009: ldstr  "Plant" 
    IL_000e: ldc.i4.1 
    IL_000f: newarr  [mscorlib]System.Object 
    IL_0014: stloc.3 
    IL_0015: ldloc.3 
    IL_0016: ldc.i4.0 
    IL_0017: ldstr  "test" 
    IL_001c: newobj  instance void class VB$AnonymousType_0`1::.ctor(!0) 
    IL_0021: stelem.ref 
    IL_0022: nop 
    IL_0023: ldloc.3 
    IL_0024: ldnull 
    IL_0025: ldnull 
    IL_0026: ldnull 
    IL_0027: call  object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.NewLateBinding::LateGet(object, 
                                class [mscorlib]System.Type, 
                                string, 
                                object[], 
                                string[], 
                                class [mscorlib]System.Type[], 
                                bool[]) 
    IL_002c: call  object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object) 
    IL_0031: stloc.1 
    IL_0032: ldloc.1 
    IL_0033: ldnull 
    IL_0034: ldstr  "LatinName" 
    IL_0039: ldc.i4.0 
    IL_003a: newarr  [mscorlib]System.Object 
    IL_003f: ldnull 
    IL_0040: ldnull 
    IL_0041: ldnull 
    IL_0042: call  object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.NewLateBinding::LateGet(object, 
                                class [mscorlib]System.Type, 
                                string, 
                                object[], 
                                string[], 
                                class [mscorlib]System.Type[], 
                                bool[]) 
    IL_0047: call  object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object) 
    IL_004c: call  void [mscorlib]System.Console::WriteLine(object) 
    IL_0051: nop 
    IL_0052: call  string [mscorlib]System.Console::ReadLine() 
    IL_0057: pop 
    IL_0058: nop 
    IL_0059: ret 
} // end of method Module1::Main

सी #:

 
    .method private hidebysig static void Main(string[] args) cil managed 
{ 
    .entrypoint 
    // Code size  299 (0x12b) 
    .maxstack 10 
    .locals init ([0] object c, 
      [1] object plant, 
      [2] class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo[] CS$0$0000) 
    IL_0000: nop 
    IL_0001: newobj  instance void [ClaySharp]ClaySharp.ClayFactory::.ctor() 
    IL_0006: stloc.0 
    IL_0007: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1f__AnonymousType0`1',object>> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site1' 
    IL_000c: brtrue.s IL_004c 
    IL_000e: ldc.i4.0 
    IL_000f: ldstr  "Plant" 
    IL_0014: ldnull 
    IL_0015: ldtoken ConsoleApplication2.Program 
    IL_001a: call  class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) 
    IL_001f: ldc.i4.2 
    IL_0020: newarr  [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo 
    IL_0025: stloc.2 
    IL_0026: ldloc.2 
    IL_0027: ldc.i4.0 
    IL_0028: ldc.i4.0 
    IL_0029: ldnull 
    IL_002a: call  class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, 
                                              string) 
    IL_002f: stelem.ref 
    IL_0030: ldloc.2 
    IL_0031: ldc.i4.1 
    IL_0032: ldc.i4.1 
    IL_0033: ldnull 
    IL_0034: call  class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, 
                                              string) 
    IL_0039: stelem.ref 
    IL_003a: ldloc.2 
    IL_003b: call  class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::InvokeMember(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, 
                                           string, 
                                           class [mscorlib]System.Collections.Generic.IEnumerable`1, 
                                           class [mscorlib]System.Type, 
                                           class [mscorlib]System.Collections.Generic.IEnumerable`1) 
    IL_0040: call  class [System.Core]System.Runtime.CompilerServices.CallSite`1 class [System.Core]System.Runtime.CompilerServices.CallSite`1f__AnonymousType0`1',object>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder) 
    IL_0045: stsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1f__AnonymousType0`1',object>> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site1' 
    IL_004a: br.s  IL_004c 
    IL_004c: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1f__AnonymousType0`1',object>> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site1' 
    IL_0051: ldfld  !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1f__AnonymousType0`1',object>>::Target 
    IL_0056: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1f__AnonymousType0`1',object>> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site1' 
    IL_005b: ldloc.0 
    IL_005c: ldstr  "test" 
    IL_0061: newobj  instance void class 'f__AnonymousType0`1'::.ctor(!0) 
    IL_0066: callvirt instance !3 class [mscorlib]System.Func`4f__AnonymousType0`1',object>::Invoke(!0, 
                                                  !1, 
                                                  !2) 
    IL_006b: stloc.1 
    IL_006c: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site2' 
    IL_0071: brtrue.s IL_00b6 
    IL_0073: ldc.i4  0x100 
    IL_0078: ldstr  "WriteLine" 
    IL_007d: ldnull 
    IL_007e: ldtoken ConsoleApplication2.Program 
    IL_0083: call  class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) 
    IL_0088: ldc.i4.2 
    IL_0089: newarr  [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo 
    IL_008e: stloc.2 
    IL_008f: ldloc.2 
    IL_0090: ldc.i4.0 
    IL_0091: ldc.i4.s 33 
    IL_0093: ldnull 
    IL_0094: call  class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, 
                                              string) 
    IL_0099: stelem.ref 
    IL_009a: ldloc.2 
    IL_009b: ldc.i4.1 
    IL_009c: ldc.i4.0 
    IL_009d: ldnull 
    IL_009e: call  class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, 
                                              string) 
    IL_00a3: stelem.ref 
    IL_00a4: ldloc.2 
    IL_00a5: call  class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::InvokeMember(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, 
                                           string, 
                                           class [mscorlib]System.Collections.Generic.IEnumerable`1, 
                                           class [mscorlib]System.Type, 
                                           class [mscorlib]System.Collections.Generic.IEnumerable`1) 
    IL_00aa: call  class [System.Core]System.Runtime.CompilerServices.CallSite`1 class [System.Core]System.Runtime.CompilerServices.CallSite`1>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder) 
    IL_00af: stsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site2' 
    IL_00b4: br.s  IL_00b6 
    IL_00b6: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site2' 
    IL_00bb: ldfld  !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1>::Target 
    IL_00c0: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site2' 
    IL_00c5: ldtoken [mscorlib]System.Console 
    IL_00ca: call  class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) 
    IL_00cf: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site3' 
    IL_00d4: brtrue.s IL_0109 
    IL_00d6: ldc.i4.0 
    IL_00d7: ldstr  "LatinName" 
    IL_00dc: ldtoken ConsoleApplication2.Program 
    IL_00e1: call  class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) 
    IL_00e6: ldc.i4.1 
    IL_00e7: newarr  [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo 
    IL_00ec: stloc.2 
    IL_00ed: ldloc.2 
    IL_00ee: ldc.i4.0 
    IL_00ef: ldc.i4.0 
    IL_00f0: ldnull 
    IL_00f1: call  class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, 
                                              string) 
    IL_00f6: stelem.ref 
    IL_00f7: ldloc.2 
    IL_00f8: call  class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::GetMember(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, 
                                          string, 
                                          class [mscorlib]System.Type, 
                                          class [mscorlib]System.Collections.Generic.IEnumerable`1) 
    IL_00fd: call  class [System.Core]System.Runtime.CompilerServices.CallSite`1 class [System.Core]System.Runtime.CompilerServices.CallSite`1>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder) 
    IL_0102: stsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site3' 
    IL_0107: br.s  IL_0109 
    IL_0109: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site3' 
    IL_010e: ldfld  !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1>::Target 
    IL_0113: ldsfld  class [System.Core]System.Runtime.CompilerServices.CallSite`1> ConsoleApplication2.Program/'o__SiteContainer0'::'p__Site3' 
    IL_0118: ldloc.1 
    IL_0119: callvirt instance !2 class [mscorlib]System.Func`3::Invoke(!0, 
                                        !1) 
    IL_011e: callvirt instance void class [mscorlib]System.Action`3::Invoke(!0, 
                                              !1, 
                                              !2) 
    IL_0123: nop 
    IL_0124: call  string [mscorlib]System.Console::ReadLine() 
    IL_0129: pop 
    IL_012a: ret 
} // end of method Program::Main 

सी # और वीबी संस्करण समान नहीं हैं। ऐसा लगता है कि सी # संस्करण बहुत कुछ करता है।

+0

एक अजीब वहाँ में कोई लैम्ब्डा है पर विचार त्रुटि है। –

+0

अभिव्यक्ति वृक्ष संकलन करते समय यह बम। इस कोड में कई अनावश्यक चालें। –

+1

क्योंकि आपके ब्लॉग का जवाब है, मुझे लगता है कि आपको इसे यहां पोस्ट करना चाहिए। – jbtule

उत्तर

2

मैं क्ले बारे में कुछ पता नहीं है, लेकिन सी # गुमनाम प्रकार और वीबी लोगों के बीच एक संभावित महत्वपूर्ण अंतर है: सी # गुमनाम प्रकार हमेशा केवल पढ़ने के गुण डिफ़ॉल्ट रूप से, जबकि, वीबी लोगों परिवर्तनशील हैं ।

केवल पढ़ने-योग्य गुणों का उपयोग समानता में और वीबी में हैश कोड पीढ़ी में किया जाता है। इन्हें Key कीवर्ड के साथ निर्दिष्ट किया जा सकता है।तो अपने सी # कोड के करीब है, वीबी होना चाहिए:

Dim c As Object = New ClayFactory 
Dim plant = c.Plant(New With { Key .LatinName = "test"}) 
Console.WriteLine(plant.LatinName) 
Console.ReadLine() 

कि आज़मा कर देखें और देखें कि क्या यह मदद करता है :)

+0

यह वही त्रुटि देता है। – chrissie1

+0

@ chrissie1: उस स्थिति में आप दोनों कंपाइलरों द्वारा उत्पन्न आईएल को देखना चाहेंगे। कौन सी रेखा त्रुटि दे रही है? –

+0

मंद संयंत्र = सी। प्लांट एक। – chrissie1

संबंधित मुद्दे

 संबंधित मुद्दे