2012-03-28 95 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;