Вы можете определить свою собственную правду и ложь с помощью методов расширения.
public static bool Falsy(this object obj) {
if(obj == null) return true;
if (obj is string)
if(obj as string == string.Empty) return true;
if(obj is byte)
if((byte)obj == 0) return true;
if(obj is sbyte)
if((sbyte)obj == 0) return true;
if(obj is short)
if((short)obj == 0) return true;
if(obj is ushort)
if((ushort)obj == 0) return true;
if(obj is int)
if((int)obj == 0) return true;
if(obj is uint)
if((uint)obj == 0) return true;
if(obj is long)
if((long)obj == 0) return true;
if(obj is ulong)
if((ulong)obj == 0) return true;
if(obj is float)
if((float)obj == 0) return true;
if(obj is double)
if((double)obj == 0) return true;
if(obj is decimal)
if((decimal)obj == 0) return true;
if(obj is IEnumerable<object>)
if((obj as IEnumerable<object>).Count<object>() == 0)
return true;
if(obj is Array)
if(((Array)obj).Length <= 0)
return true;
if(obj is ObjectId)
if(((ObjectId)obj).Pid == 0) return true;
if(obj is System.Collections.ObjectModel.ObservableCollection<M>)
if(((ObservableCollection<M>)obj).Count <= 0) return true;
return false;
}
public static bool Truthy(this object obj) {
return !Falsy(obj);
}
Таким образом, вы можете сделать что-то вроде:
if (customerList.Falsy ()) {throw new Exception («Не может быть нулевым или пустым.»); }
Я закончил тем, что использовал ниже отрывка, и он работает в большинстве браузеров, не протестированных в IE все же.
function downloadURI(uri, name) {
let link = document.createElement("a");
link.download = name;
link.href = uri;
link.style.display = "none";
document.body.appendChild(link);
if (typeof MouseEvent !== "undefined") {
link.dispatchEvent(new MouseEvent("click"));
} else {
link.click();
}
document.body.removeChild(link);
}
let data = [{email: "test@domain.com", name: "test"}, {email: "anothertest@example.com", name: "anothertest"}];
let json = JSON.stringify(data);
let blob = new Blob([json], {type: "application/json"});
let url = window.URL.createObjectURL(blob);
downloadURI(url, "file.json");
window.URL.revokeObjectURL(url);