C#/Winform
                
              (.NET) null 값 미리 체크하기
                kjun.kr
                 2017. 9. 17. 21:11
              
              
                    
        728x90
    
    
          private dynamic SafeField(Func<dynamic> func)
{
try
{
return func();
}
catch (System.NullReferenceException ex)
{
try
{
return func();
}
catch (System.NullReferenceException ex)
{
                 return "(Not set)";
            }
            catch (System.InvalidOperationException ex)
{
                //LINQ 시퀀스에 요소가 없습니다.
                //if(ex.Message == "시퀀스에 요소가 없습니다.") .... todo?
                return "(Not data)";
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
사용법
txtREQUIREMENT.Text = SafeField(() => data.REQUIREMENT.NAME);
728x90