헤드 필드와 일반 유형 데이터를 가져 오는 모델이 있습니다.
public class RootApiModel<T> where T : class
{
public string @event { get; set; }
public string timestamp { get; set; }
public string token { get; set; }
public string signature { get; set; }
public int status { get; set; }
public T data { get; set; }
}
내 문제는 RequiredFieldsControl
클래스 에서 필수 필드를 확인 하고 있습니다. 코드를 반복하지 않으려면 헤드 컨트롤과 일반 데이터 컨트롤을 별도로 확인하고 싶습니다.하지만 어떻게 HeadRequiredFields
메서드에 대한 참조를 얻을 수 있습니까?
"RootApiModel<object> item"
또는
"RootApiModel<T> item"
변환 할 수 없음 경고가 표시됩니다.
public static class RequiredFieldsControl
{
private static void HeadRequiredFields(RootApiModel<object> item)
{
if (string.IsNullOrEmpty(item.@event))
throw new Exception("Event Alanı Zorunludur");
if (string.IsNullOrEmpty(item.timestamp))
throw new Exception("Timestamp Alanı Zorunludur");
if (string.IsNullOrEmpty(item.token))
throw new Exception("Token Alanı Zorunludur");
if (string.IsNullOrEmpty(item.signature))
throw new Exception("Signature Alanı Zorunludur");
if (item.status <= 0)
throw new Exception("Status Alanı Zorunludur");
}
public static void BuildingControl(RootApiModel<BuildingApiModel> buildingItem)
{
HeadRequiredFields(buildingItem);
if (buildingItem.data.ReferenceID<=0)
throw new Exception("BuildingReferenceID Alanı Zorunludur");
if (string.IsNullOrEmpty(buildingItem.data.BuildingName))
throw new Exception("BuildingName Alanı Zorunludur");
}
public static void BlockControl(RootApiModel<BlockApiModel> blockItem)
{
HeadRequiredFields(blockItem);
if (blockItem.data.BuildingReferenceID <= 0)
throw new Exception("BuildingReferenceID Alanı Zorunludur");
if (blockItem.data.BlockReferenceID <= 0)
throw new Exception("BlockReferenceID Alanı Zorunludur");
if (string.IsNullOrEmpty(blockItem.data.BlockName))
throw new Exception("BlockName Alanı Zorunludur");
}
}
HeadRequiredFields는 다음과 같이 보일 것입니다.
private static void HeadRequiredFields<T>(RootApiModel<T> item) where T : class
이것은 <T>
참조 유형으로 제한 되므로 값 유형은 허용되지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다