小编neo*_*pir的帖子

配置在抽象类上定义的Autofac委托工厂

我正在研究一个C#项目.我正在试图摆脱具有大switch声明的Factory类.

我想配置Autofac以便能够基于参数构造依赖关系,从而允许Autofac取代Factory.

我查看了Autofac wikiDelegateFactories页面,但我无法弄清楚如何将模式应用于抽象类.以下是一些显示情况的代码:

public enum WidgetType
{
    Sprocket,
    Whizbang
}

public class SprocketWidget : Widget
{
}

public class WhizbangWidget : Widget
{
}

public abstract class Widget
{
    public delegate Widget Factory(WidgetType widgetType);
}

public class WidgetWrangler
{
    public Widget Widget { get; private set; }

    public WidgetWrangler(IComponentContext context, WidgetType widgetType)
    {
        var widgetFactory = context.Resolve<Widget.Factory>();
        Widget = widgetFactory(widgetType);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要它,如果我要说new WidgetWrangler(context, WidgetType.Sprocket),它的Widget财产将是一个SpocketWidget.

当我尝试这个时,我得到的错误表明Widget.Factory …

c# delegates factory autofac

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

在Gradle构建脚本中访问Teamcity内部版本号

如何在Teamcity执行的Gradle脚本中访问内部版本号和VCS结帐号码?

在Ant我可以分别使用${build.number}${build.vcs.number.1}.

谢谢.

teamcity properties build gradle

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

传递一个变量来捕获块

基本上我有一个字符串errorMessage,我想把它传递给catch块.请帮忙.

[WebMethod]
public List<SomeResult> Load(string userName)
{
   string errorMessage;
    using (VendorContext vendorContext = new VendorContext())
    {
         // ....
          foreach(....)
          {
               if(something happens)
                  errorMessage = "Vote Obama";
                else
                  errorMessage ="vote Romney";
              // blah
               try
                     {
                        // blah         
                     }
               catch (Exception e)
               {
                    logger.Trace(errorMessage);
               }
          }
     }
 }  
Run Code Online (Sandbox Code Playgroud)

更新:

错误:使用未分配的局部变量'errorMessage'

c# web-services c#-4.0

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

提前终止递归迭代器块方法

我有一个方法,使用递归函数输出数组的所有排列:

    /// <summary>
    /// Yields a sequence of all permutations in lexical order
    /// </summary>
    /// <typeparam name="T">Type of item in the input sequence</typeparam>
    /// <param name="input">The initial sequence</param>
    /// <returns>A sequence of all permutations in lexical order</returns>
    public IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> input) 
    {
        var list = input.ToList();
        list.Sort(); // into lexical order

        if (list.Count > 2)
        {
            foreach (var item in list)
            {
                var itemArray = new[] {item};
                T[] otherItems = list.Except(itemArray).ToArray();
                foreach (var permutation in Permute(otherItems))
                    yield return itemArray.Concat(permutation).ToArray(); …
Run Code Online (Sandbox Code Playgroud)

c# recursion nunit yield-return

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

如何断言预期会出现异常

我在使用 .NET Core 2.0 运行 F# 的 Mac 上。

我有一个如下所示的函数:

let rec evaluate(x: string) =
  match x with
  // ... cases
  | _ -> failwith "illogical"
Run Code Online (Sandbox Code Playgroud)

我想编写一个 Expecto 测试来验证异常是否按预期抛出,大致如下:

// doesn't compile
testCase "non-logic" <| fun _ ->
  Expect.throws (evaluate "Kirkspeak") "illogical" 
Run Code Online (Sandbox Code Playgroud)

错误是

该表达式的类型应为“unit -> unit”,但这里的类型为“char”

unit -> unit让我这类似于Assert.Fail,这不是我想要的。

由于对 F# 和 Expecto 有点陌生,我无法找到断言异常按预期抛出的工作示例。有人有吗?

f# .net-core expecto

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

在隐藏字段中存储列表会导致奇怪的结果

public ActionResult DoSomething()
{
return View("Index", new IndexModel { Foo = new List<string>() { "*" });
}
Run Code Online (Sandbox Code Playgroud)

其中Index.cshtml有一个包含的表单 @Html.HiddenFor(m => m.Foo)

public ActionResult ProcessForm(IndexModel model)
{
}
Run Code Online (Sandbox Code Playgroud)

在ProcessForm中你的model.Foo包含一个字符串,其中包含:

System.Collections.Generic.List`1[System.String]
Run Code Online (Sandbox Code Playgroud)

我感到很困惑...

hidden-field razor asp.net-mvc-3

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

升级到4.0:命名空间"System"中不存在类型或命名空间名称"Linq"(您是否缺少程序集引用?)

我最近将我的解决方案从.net framework 3.5升级到4.0.一切都很好,除了它在我的网站项目中给我一些错误.

The type or namespace name 'Linq' does not exist in the namespace 'System' 
(are you missing an assembly reference?)

The type or namespace name 'Linq' does not exist in the namespace 'System.Xml'
(are you missing an assembly reference?)

The type or namespace name 'Script' does not exist in the namespace 'System.Web' 
(are you missing an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

这是我的web.config看起来像:

<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, …
Run Code Online (Sandbox Code Playgroud)

linq reference upgrade .net-4.0 visual-studio-2010

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

执行LambdaExpression时遇到麻烦

H.我正在尝试构建linq查询,动态生成动态发送字段的自定义排序查询.

我是建构逻辑

Expression<Func<string, int>> SpaceStringSortExpression = (a) => a.StartsWith(" ") ? 2 : 1; 
Run Code Online (Sandbox Code Playgroud)

此代码(SpaceStringSortExpression.ToString())的签名是"a => IIF(a.StartsWith(\"\"),2,1)"

为了做到这一点,我做了:

ParameterExpression parameter = Expression.Parameter(typeof(TSource), "p1");
            Expression orderByProperty = Expression.Property(parameter, propertyName);
            ConstantExpression c = Expression.Constant(" ", typeof(string));
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            Expression call = Expression.Call(orderByProperty, mi, c);
            Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(call, parameter);
            ConditionalExpression t = Expression.IfThenElse(call, Expression.Constant(2), Expression.Constant(1));
            //t.tostring() - IIF(p1.Login.StartsWith(" "), 2, 1)
            LambdaExpression callt = Expression.Lambda(t, new[] { parameter });
            //callt.tostring() = p1 => …
Run Code Online (Sandbox Code Playgroud)

linq expression-trees

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

如何从 c# 中的 3x3 单应矩阵获得旋转、平移、剪切

我计算了 3x3 单应矩阵,我需要获得旋转、平移、剪切和缩放以将它们用作 windows8 媒体元素属性中的参数?!

c# transformation homography windows-8

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

将实体列表保存到数据库-MVC

我想我几乎可以完成这项工作,但是我不知道该如何完成。

模型:

 public class Location
 {
    public int LocationId { get; set; }
    public string SiteCode { get; set; }
    public int PersonId { get; set; }
    public int IncidentId { get; set; }
  }
Run Code Online (Sandbox Code Playgroud)

查看模型

    public List<Location> LocationList { get; set; }
Run Code Online (Sandbox Code Playgroud)

控制器:

    [HttpPost]
    public ActionResult AddRecord(RecordViewModel model)
    {
        if (ModelState.IsValid)
        {
            Location location;
            foreach (var loc in model.LocationList)
            {
                location = new Location
                {
                    PersonId = model.PersonId,
                    SiteCode = loc.SiteCode,
                    IncidentId = loc.IncidentId
                };
            }

            using (var db …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework asp.net-mvc-3

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