2015-03-02 17 views
6

Kiểm tra trong C# một đối tượng VB.NET for null cho bất ngờ lỗi biên dịch:Kiểm tra trong C# một đối tượng VB.NET for null cho bất ngờ lỗi biên dịch

// Cannot compile: 
var basePackage = BasePackage.GetFromID(id); // GetFromID is from VB.NET 
if (basePackage != null) // Errormesage: "Cannot implicitly convert 'object' to 'bool' 
{ 
} 

Resharper của sửa chữa đề nghị:

// Can compile 
if ((bool) (basePackage != null)) 
{ 
    linkedGroups = basePackage.GetLinkedGroups(); 
} 

Tôi có một đồng nghiệp đã làm điều này trong một năm mà không có bất kỳ vấn đề gì. Đồng nghiệp của tôi đang sử dụng Visual Studio 2012 và tôi đang sử dụng Visual Studio 2013. Có thể là một số loại cài đặt không?

Tại sao là basePackage != null an object?

Tôi biết VB.NET có Nothing trong đó C# có null.

CẬP NHẬT: BasePackage được thừa kế từ lớp khác: Tôi không biết điều đó có giúp ích gì không.

Public Shared Operator =([object] As CMSObject, type As System.Type) 
    Return [object].GetType Is type 
End Operator 

Public Shared Operator <>([object] As CMSObject, type As System.Type) 
    Return [object].GetType IsNot type 
End Operator 

Public Shared Operator =([object] As CMSObject, o As Object) 
    Return [object].GetType Is o 
End Operator 

Public Shared Operator <>([object] As CMSObject, o As Object) 
    Return [object].GetType IsNot o 
End Operator 

SOLUTION: Khi tôi outcomment hai nhà khai thác những C# đang làm việc tốt một lần nữa.

Public Shared Operator =([object] As CMSObject, type As System.Type) 
    Return [object].GetType Is type 
End Operator 

'Public Shared Operator <>([object] As CMSObject, type As System.Type) 
' Return [object].GetType IsNot type 
'End Operator 

Public Shared Operator =([object] As CMSObject, o As Object) 
    Return [object].GetType Is o 
End Operator 

'Public Shared Operator <>([object] As CMSObject, o As Object) 
' Return [object].GetType IsNot o 
'End Operator 

Giải pháp cuối cùng Đã thêm loại trong VB.NET. Không cần cho C# cast sau đó.

Public Shared Operator =([object] As CMSObject, type As System.Type) **As Boolean** 
    Return [object].GetType Is type 
End Operator 

Public Shared Operator <>([object] As CMSObject, type As System.Type) **As Boolean** 
    Return [object].GetType IsNot type 
End Operator 

Public Shared Operator =([object] As CMSObject, o As Object) **As Boolean** 
    Return [object].GetType Is o 
End Operator 

Public Shared Operator <>([object] As CMSObject, o As Object) **As Boolean** 
    Return [object].GetType IsNot o 
End Operator 
+0

Chữ ký của 'GetFromID' trông giống như thế nào trong VB? –

+0

Chức năng chia sẻ công khai GetFromID (ByVal jobID As Integer) Là BasePackage – radbyx

+0

'BasePackage' trông như thế nào? Nó có chứa bất kỳ toán tử nào không? – CodeCaster

Trả lời

2

tôi lấy mẫu vb của bạn, combpiled nó thành một dll và dịch ngược nó để C# Đó là cách bạn khai thác trông

public static object operator ==(Class1 @object, Type type) 
{ 
    return (object) (bool) (@object.GetType() == type ? 1 : 0); 
} 

public static object operator !=(Class1 @object, Type type) 
{ 
    return (object) (bool) (@object.GetType() != type ? 1 : 0); 
} 

public static object operator ==(Class1 @object, object o) 
{ 
    return (object) (bool) (@object.GetType() == o ? 1 : 0); 
} 

public static object operator !=(Class1 @object, object o) 
{ 
    return (object) (bool) (@object.GetType() != o ? 1 : 0); 
} 

Vì vậy, nó chỉ là do chữ ký điều hành quá tải lạ.

Bạn nhận xét các nhà khai thác "Không bình đẳng", bây giờ nó dường như làm việc, nhưng bạn sẽ nhận được lỗi tương tự khi bạn viết một cái gì đó giống như

if ((basePackage == null)) 
// etc. 

Các giải pháp sẽ được, như đề xuất trong các ý kiến, để xác định bạn chữ ký quá tải của toán tử là Boolean.

Các vấn đề liên quan