2011-07-26 59 views
4

我期望能夠替換參數的對象引用而不必使用ref關鍵字。不使用ref關鍵字替換參數的ref(使用IL)

我避免使用ref的原因是爲了保留集合初始化器的調用,它尋找Add(T item)方法,我需要讓集合類用它的接口的不同實現替換引用。

我嘗試了幾種不同的方式來做到這一點。首先,我嘗試使用未記錄的關鍵字__makeref,__refvalue__reftype

其次,我試圖創建一個DynamicMethod與一些IL試圖模仿我看到一個ref參數反彙編類似的調用觀察。

下面是一些代碼來演示:

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Reflection.Emit; 
using System.Reflection; 
interface IRecord 
{ 
    string Name { get;} 
} 
class ImpA : IRecord 
{ 
    public string Name { get { return "Implementation A"; } } 
} 
class ImpB : IRecord 
{ 
    public string Name { get { return "Implementation B"; } } 
} 
class RecordList<T> : IEnumerable<T> 
{ 
    //// Standard Add method (of course does not work) 
    //public void Add(T item) 
    //{ 
    // item = (T)(object)new ImpB(); 
    //} 

    // ref method (works great but the signature cannot be 
    // used by the collection initializer) 
    public void Add(ref T item) 
    { 
     IRecord newItem = new ImpB(); 
     item = (T)newItem; 
    } 

    //// Using System.TypedReference (does not work) 
    //public void Add(T item) 
    //{ 
    // T newItem = (T)(object)new ImpB(); 
    // TypedReference typedRef = __makeref(item); 
    // __refvalue(typedRef, T) = newItem; 
    //} 

    // Using Reflection.Emit DynamicMethod (This method should work but I need help) 
    public void Add(T item) 
    { 
     IRecord newItem = new ImpB(); 

     System.Reflection.MethodAttributes methodAttributes = 
       System.Reflection.MethodAttributes.Public 
      | System.Reflection.MethodAttributes.Static; 

     DynamicMethod dm = new DynamicMethod("AssignRef", 
      methodAttributes, 
      CallingConventions.Standard, 
      null, 
      new Type[] { typeof(IRecord), typeof(IRecord) }, 
      this.GetType(), 
      true); 

     ILGenerator generator = dm.GetILGenerator(); 
     // IL of method 
     //public static void Add(ref item, ref newItem) 
     //{ 
     // item = newItem; 
     //} 
     // -- Loading Params (before call to Add() -- 
     //L_002b: ldloca.s sb // this is the ref variable 
     //L_002d: ldloc.2 // The other variable 
     // -- Add method IL -- 
     //L_0000: nop 
     //L_0001: ldarg.0 
     //L_0002: ldarg.1 
     //L_0003: stind.ref 
     //L_0004: ret 

     generator.Emit(OpCodes.Ldarga_S, 0); 
     generator.Emit(OpCodes.Ldarg_1); 
     generator.Emit(OpCodes.Stind_Ref); 
     generator.Emit(OpCodes.Ret); 

     Action<IRecord, IRecord> AssignRef = 
      (Action<IRecord, IRecord>)dm.CreateDelegate(
      typeof(Action<IRecord, IRecord>)); 

     AssignRef((IRecord)item, (IRecord)newItem); 
    } 


    public IEnumerator GetEnumerator() 
    { 
     throw new NotImplementedException(); 
    } 
    IEnumerator<T> IEnumerable<T>.GetEnumerator() 
    { 
     throw new NotImplementedException(); 
    } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     IRecord imp = new ImpA(); 
     Console.WriteLine("Original implementation: {0}\n", imp.Name); 

     // Calls Add Implicitly 
     RecordList<IRecord> records = new RecordList<IRecord> { imp }; 
     // Prints "Implementation A" 
     Console.WriteLine("After Add Method: {0}", imp.Name); 

     records.Add(ref imp); // Explicit call with ref 
     // Prints "Implementation B" 
     Console.WriteLine("After Add Ref method: {0}\n", imp.Name); 
    } 
} 

謝謝。

+5

你正在嘗試製造一些真正難看的東西。你確定你需要/想要這個嗎? –

+0

我發佈的代碼片段僅用於演示幫助解釋我的問題的概念。我意識到它可以用來創造醜陋的東西 - 謝謝。 – RepDbg

回答

8

我期望能夠替換參數的對象引用而不必使用ref關鍵字。

這根本不會發生;當你的(非ref)方法被調用時,CLR會創建一個你的方法收到的傳入引用的副本。雖然您的方法可以修改該引用的內容,但無論您嘗試使用未記錄的關鍵字或使用未記錄的關鍵字的任何欺騙,它的都絕對不能訪問以進行復制的引用(調用方法傳入的引用)或聰明的CIL。

另外,爲什麼你試圖將傳遞給集合初始值設定項的參數替換爲我以外。它聞到代碼,它不應該做的事情。

+0

感謝您的答覆 - 我認爲這是在託管c + +(我錯了嗎?)完成。那麼它怎麼會成爲CLR的限制呢?如果我正在使用一個接口,爲什麼切換實現成爲一個問題? – RepDbg

+2

我不知道允許函數修改非引用參數的語言。這不能在.NET中完成(因爲它不受CLR支持),這意味着它無法在託管C++或C++/CLI中完成。 –