2012-03-28 66 views
0

我想了解如何創建自定義控件,工具欄。使用.NET Reflector,我試圖「重寫」ToolStripDesigner類(現在,這意味着只將代碼從反射器複製到Visual Studio)。因爲它使用System.Design.dll內部的許多類,所以我不得不用Reflector複製更多的類。在System.Windows.Forms.Design.OleDragDropHandler類,我發現這個代碼:標誌枚舉的二進制操作

DragDropEffects allowedEffects = DragDropEffects.Move | DragDropEffects.Copy; 
for (int i = 0; i < components.Length; i++) 
{ 
    InheritanceAttribute attribute = (InheritanceAttribute) TypeDescriptor.GetAttributes(components[i])[typeof(InheritanceAttribute)]; 
    if (!attribute.Equals(InheritanceAttribute.NotInherited) && !attribute.Equals(InheritanceAttribute.InheritedReadOnly)) 
    { 
     allowedEffects &= ~DragDropEffects.Move; 
     allowedEffects |= 0x4000000;  // this causes error 
    } 
} 

DragDropEffects枚舉是公共​​的,與這些領域:

[Flags] 
public enum DragDropEffects { 
    Scroll = -2147483648, // 0x80000000 
    All = -2147483645,  // 0x80000003 
    None = 0, 
    Copy = 1, 
    Move = 2, 
    Link = 4, 
} 

你可以看到,有一個與價值的第一塊顯示任何領域的代碼(0x4000000)。 另外,這段代碼在VS中拋出錯誤:operator |= cannot be applied to operands of type System.Windows.Forms.DragDropEffects and int

所以我的問題是 - 這是怎麼編譯的?或者也許.NET Reflector在反編譯過程中犯了一個錯誤?有沒有什麼辦法可以繞過它(在allowedEffects變量中沒有丟失這個奇怪的,未命名的信息)?

回答

1

的整數被強制轉換爲一個DragDropEffects對象,因爲在這裏看到在ILSpy:

enter image description here

+0

感謝提到ILSpy,有時間學習使用另一種工具;) – 2012-03-28 11:58:50

2

它確實看起來像反射器錯過了一些東西。爲了使編譯你需要顯式轉換0x4000000DragDropEffects:它

allowedEffects &= ~DragDropEffects.Move; 
allowedEffects |= (DragDropEffects)0x4000000; 
+0

我不知道,有可能投未在枚舉defini指定的值灰。謝謝 – 2012-03-28 12:00:30

1

切換到這一點,它會編譯:

allowedEffects |= (DragDropEffects)0x4000000;