Why is this not cls-compliant?
-
22-04-2021 - |
Frage
From what I've been reading online, if the field is private
it can start with a leading _
. However when I do the following it complains? Is it because I'm returning the private field? Doesn't make sense to me since anything consuming this has no idea about _myObject so why would it matter?
private MyBusinessObject _myObjectBO;
protected MyBusinessObject MyObjectBO
{
get { return _myObjectBO ?? (_myObjectBO= new MyBusinessObject()); }
}
Lösung
The message is stating that the property's type is not compliant.
Check the MyBusinessObject
class; many developers forgot to add [assembly: CLSCompliant(true)]
(unfortunately, it isn't part of the standard template)
Andere Tipps
Nothing about this is inherently not CLS Compliant. What does MyObject look like? I tested with the following code, and got no CLS compliance warnings at compile time:
[CLSCompliant(true)]
public class Program
{
private MyObject _myObject;
[CLSCompliant(true)]
public MyObject ComplaintTypeBO
{
get { return _myObject ?? (_myObject = new MyObject()); }
}
static void Main(string[] args)
{
}
}
[CLSCompliant(true)]
public class MyObject
{
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow