我有一段代码:
using (StreamReader stream = new StreamReader(file.OpenRead(), Encoding))
{
char[] buffer = new char[chunksize];
while (stream.Peek() >= 0)
{
int readCount = stream.Read(buffer, 0, chunksize);
yield return new string(buffer, 0, readCount);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我必须使用try-catch块来包围它
try
{
using (StreamReader stream = new StreamReader(file.OpenRead(), Encoding))
{
char[] buffer = new char[chunksize];
while (stream.Peek() >= 0)
{
int readCount = stream.Read(buffer, 0, chunksize);
yield return new string(buffer, 0, readCount);
}
}
}
catch (Exception ex)
{
throw ExceptionMapper.Map(ex, file.FullName)
}
Run Code Online (Sandbox Code Playgroud)
我看不出有什么方法可以做我想做的事.
编辑 该方法具有签名 …
我问自己,如果扩展方法在同一个扩展类中使用另一个,那么它是否是一个好的设计.
public class ClassExtensions
{
public static bool IsNotNull<T>(this T source)
where T : class
{
return !source.IsNull();
}
public static bool IsNull<T>(this T source)
where T : class
{
return source == null;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 感谢您的回答.对不好的样品感到抱歉.
我有这样的数据结构
public class Employee
{
public string Name { get; set; }
public IEnumerable<Employee> Employees { get; private set; }
// ...
}
Run Code Online (Sandbox Code Playgroud)
现在我需要遍历整个结构并对每个项目执行一个方法.
如何在IEnumerable上为这样的遍历函数创建扩展.
Wonderfull会是这样的
employeList.Traverse(e => Save(e), e.Employees.Count > 0);
Run Code Online (Sandbox Code Playgroud)
或者这是不可能的,我必须在我的业务逻辑中创建一个特殊的方法?
非常感谢.
我需要安装一些文件C:\Documents and Settings\currentUser\SomeFolder.我找不到一些buildin函数/变量.
有人可以帮我/告诉我<DirectoryRef Id="TARGETDIR">我的问题的结构或解决方案吗?
谢谢.
我被问到什么是错的/如何修复以下场景
Customer customer = null;
customer.WhenNull(c => new Customer())
.Foo();
// instead of
Customer customer = null;
if (customer == null)
{
customer = new Customer();
}
customer.Foo();
Run Code Online (Sandbox Code Playgroud)
一位开发人员向我发送了他的WhenNull扩展版本
public static T WhenNull<T>(this T source, Action<T> nullAction)
{
if (source != null)
{
return source;
}
if (nullAction == null)
{
throw new ArgumentNullException("nullAction");
}
nullAction(source);
return source;
}
Run Code Online (Sandbox Code Playgroud)
他的问题/意图是,他不想在lambda表达式中指定底层对象(在本例中为"customer")
Customer customer = null;
customer.WhenNull(c => customer = new Customer())
.Foo();
Run Code Online (Sandbox Code Playgroud)
我以为这不可能做到.
它是否正确?
我有以下(这里简化的)代码,我想用FakeItEasy测试.
public class ActionExecutor : IActionExecutor
{
public void TransactionalExecutionOf(Action action)
{
try
{
// ...
action();
// ...
}
catch
{
// ...
Rollback();
}
}
public void Commit()
{ }
public void Rollback()
{ }
}
public class Service : IService
{
private readonly IRepository _repository;
private readonly IActionExecutor _actionExecutor;
// ctor for CI
public void ServiceMethod(string name)
{
_actionExecutor.TransactionalExecutionOf(() =>
{
var item = _repository.FindByName(ItemSpecs.FindByNameSpec(name));
if (item == null) throw new ServiceException("Item not found");
item.DoSomething(); …Run Code Online (Sandbox Code Playgroud) 我已经读过我应该创建并重用委托来获取对象属性的值.
我使用此代码来创建Delegate
var objParm = Expression.Parameter(property.DeclaringType, "o");
Type delegateType = typeof(Func<,>).MakeGenericType(property.DeclaringType, property.PropertyType);
var lambda = Expression.Lambda(delegateType, Expression.Property(objParm, property.Name), objParm);
return lambda.Compile()
Run Code Online (Sandbox Code Playgroud)
现在我只找到在"DynamicInvoke"调用中使用委托的方法.
现在我想将调用更改为"调用"导致性能原因.
我试过了
Delegate.Method.Invoke(invokedObject, null);
Run Code Online (Sandbox Code Playgroud)
但是在通话结束后我得到了例外
MethodInfo must be a RuntimeMethodInfo.
Run Code Online (Sandbox Code Playgroud)
我有每个属性的缓存类,我可以存储动态创建的委托.
我应该怎么做才能使用"普通"调用?
非常感谢.