سؤال

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()); }
}
هل كانت مفيدة؟

المحلول

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)

نصائح أخرى

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
{
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top