В C # / C ++ я могу добиться этого, используя троичный оператор, хотя код будет отвратительным. Вы уверены, что хотите использовать это?
if ((myobject! = Null? (Myobject.myproperty! = Null? (Myobject.myproperty.myotherproperty! = Null? Myobject.myproperty.myotherproperty.value: null): null): null )! = ноль)
class MyOtherProperty
{
public string value;
}
class MyProperty
{
public MyOtherProperty myotherproperty;
}
class MyObject
{
public MyProperty myproperty;
}
Тестовый код My Unit:
[TestMethod()]
public void TestTernaryOperator()
{
MyObject myobject = new MyObject();
Debug.WriteLine (string.Format ("{0}, {1}", myobject != null, myobject.myproperty != null));
Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject)));
myobject.myproperty = new MyProperty();
Debug.WriteLine (string.Format ("{0}, {1}, {2}", myobject != null, myobject.myproperty != null, myobject.myproperty.myotherproperty != null));
Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject)));
myobject.myproperty.myotherproperty = new MyOtherProperty ();
Debug.WriteLine (string.Format ("{0}, {1}, {2}, {3}", myobject != null, myobject.myproperty != null, myobject.myproperty.myotherproperty != null, myobject.myproperty.myotherproperty.value != null));
Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject)));
myobject.myproperty.myotherproperty.value = "Hello world";
Debug.WriteLine(string.Format("{0}, {1}, {2}, {3}", myobject != null, myobject.myproperty != null, myobject.myproperty.myotherproperty != null, myobject.myproperty.myotherproperty.value != null));
Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject)));
}
bool IsNotNull(MyObject myobject)
{
bool isNotNull = (myobject != null ? (myobject.myproperty != null ? (myobject.myproperty.myotherproperty != null ? myobject.myproperty.myotherproperty.value : null) : null) : null) != null;
return isNotNull;
}