小编Khh*_*Khh的帖子

使用try catch返回yield,我该如何解决它

我有一段代码:

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)

我看不出有什么方法可以做我想做的事.

编辑 该方法具有签名 …

c# try-catch yield-return .net-3.5

21
推荐指数
5
解决办法
3万
查看次数

扩展方法在同一个扩展类中调用另一个 - 好的设计?

我问自己,如果扩展方法在同一个扩展类中使用另一个,那么它是否是一个好的设计.

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)

编辑 感谢您的回答.对不好的样品感到抱歉.

c# extension-methods

9
推荐指数
1
解决办法
2836
查看次数

遍历列表,执行一个方法:扩展可能吗?

我有这样的数据结构

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)

或者这是不可能的,我必须在我的业务逻辑中创建一个特殊的方法?

非常感谢.

.net c# linq lambda .net-3.5

4
推荐指数
1
解决办法
1484
查看次数

来自当前用户的MyDocuments,Wix中的buildin变量?

我需要安装一些文件C:\Documents and Settings\currentUser\SomeFolder.我找不到一些buildin函数/变量.

有人可以帮我/告诉我<DirectoryRef Id="TARGETDIR">我的问题的结构或解决方案吗?

谢谢.

deployment wix wixlib

3
推荐指数
1
解决办法
2319
查看次数

扩展"WhenNull"检查null并通过lambda初始化底层对象

我被问到什么是错的/如何修复以下场景

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)

我以为这不可能做到.
它是否正确?

c# extension-methods .net-3.5

3
推荐指数
1
解决办法
170
查看次数

使用FakeItEasy的第一步和Action类型的问题

我有以下(这里简化的)代码,我想用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)

action fakeiteasy c#-4.0

3
推荐指数
1
解决办法
4544
查看次数

Delegate.DynamicInvoke太慢,如何更改调用Invoke?

我已经读过我应该创建并重用委托来获取对象属性的值.

我使用此代码来创建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)

我有每个属性的缓存类,我可以存储动态创建的委托.

我应该怎么做才能使用"普通"调用?

非常感谢.

c# delegates dynamic invoke .net-3.5

1
推荐指数
1
解决办法
3423
查看次数