2011-10-20 9 views
5

Mam następujący kod w mojej aplikacji:Code Analysis CA1060 Fix

[DllImport("user32.dll")] 
private static extern int GetWindowLong(IntPtr hwnd, int index); 

[DllImport("user32.dll")] 
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); 

[DllImport("user32.dll")] 
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, 
       int x, int y, int width, int height, uint flags); 

[DllImport("user32.dll")] 
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg, 
       IntPtr wParam, IntPtr lParam); 

otrzymuję następujące ostrzeżenie z kodu Analiza (FxCop):

CA1060: Microsoft.Design: ponieważ jest metodą P/Invoke, "IconHelper.GetWindowLong (IntPtr, int)" należy zdefiniować w klasie o nazwie NativeMethods, SafeNativeMethods lub UnsafeNativeMethods.

Czy ktoś może mi powiedzieć, w której klasie powinienem je umieścić? Nie wiem, czy jest to Native, SafeNative czy UnsafeNative.

+0

możliwe duplikat [Jak wiedzieć, czy metoda jest rodzimy bezpieczne/niebezpieczne?] (Http://stackoverflow.com/questions/4511418/how-to-know-if-native -method-is-safe-dangerous-safe) – dtb

+2

[FAQ: Jak naprawić naruszenie klasy MovePInvokesToNativeMethodsClass?] (http://blogs.msdn.com/b/codeanalysis/archive/2007/01/14/faq-how- do-i-fix-a-violation-of-movepinvokestonativemethodsclass.aspx) – dtb

Odpowiedz

4

Spróbuj przenieść je wszystkie do klasy NativeMethod będzie rozwiązać problem

Twój kod powinien wyglądać tak po zamontowaniu go

public class NativeMethods { 
[DllImport("user32.dll")] 
private static extern int GetWindowLong(IntPtr hwnd, int index); 

[DllImport("user32.dll")] 
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); 

[DllImport("user32.dll")] 
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, 
       int x, int y, int width, int height, uint flags); 

[DllImport("user32.dll")] 
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg, 
       IntPtr wParam, IntPtr lParam); 
} 

Pamiętaj, aby zmienić wszystkie miejsca, gdzie nazywają te metody

Przed zmianą

SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam) 

powinny być

+1

Czy metody nie powinny być publiczne? – JohnSaps

+0

W powyższym kodzie metody powinny być "wewnętrzne". Ostrzeganie o "publicznym" alarmie "CA1401 P/Wywołania nie powinny być widoczne" – Sielu