Funcを使用してこれを処理するというアイデアはすでにありますが、これは私にとって最良の解決策のように思えます。私はあなたの意図にいくつかの仮定を立て、あなたがそのラベルテキストを取得しようとしていると仮定したので、そのように書きました。
private void YourFunction
{
Type TrType = this.GetType();
MooClass Moo = new MooClass();
LabelTypeEnum LabelType = LabelTypeEnum.something;
ShowIf(Moo, TrType, LabelType, new Object[] { Moo.Foo, Moo.Foo2, Moo.Foo3 }, a => a.Foo.DangerousNullRef + " - " + a.Foo.AnotherPossibleNullRef);
}
void ShowIf(MooClass Moo, Type t, LabelTypeEnum LabelType, IEnumerable<object> PreCheckNullsValues, Func<MooClass, string> mc )
{
if (PreCheckNullsValues.Any(a => a == null))
Show(t, LabelType, mc(Moo));
else
DontShowField(t);
}
サポートするコードの想定されるスケルトンは次のとおりです。
enum LabelTypeEnum
{
something
}
class MooClass
{
public FooClass Foo { get; set; }
}
class FooClass
{
public object DangerousNullRef { get; set; }
public object AnotherPossibleNullRef { get; set; }
}
private void Show(Type TrType, LabelTypeEnum LabelType, string p) { }
private void DontShowField(Type TrType) { }
その後、アクションを使用してプロパティに安全にアクセスできます。