Innovenergy_trunk/csharp/App/SchneiderMeterDriver/Utils.cs

49 lines
958 B
C#

namespace InnovEnergy.App.SchneiderDriver;
public static class Utils
{
public static IEnumerable<T> TryWhere<T>(this IEnumerable<T> src, Func<T, Boolean> predicate)
{
foreach (var e in src)
{
var ok = false;
try
{
ok = predicate(e);
}
catch
{
// ignored
}
if (ok)
yield return e;
}
}
public static IEnumerable<R> TrySelect<T,R>(this IEnumerable<T> src, Func<T, R> map)
{
foreach (var e in src)
{
var ok = false;
var result = default(R);
try
{
result = map(e);
ok = true;
}
catch
{
// ignored
}
if (ok)
yield return result!;
}
}
}