我正在研究一个C#项目.我正在试图摆脱具有大switch声明的Factory类.
我想配置Autofac以便能够基于参数构造依赖关系,从而允许Autofac取代Factory.
我查看了Autofac wiki的DelegateFactories页面,但我无法弄清楚如何将模式应用于抽象类.以下是一些显示情况的代码:
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 …
如何在Teamcity执行的Gradle脚本中访问内部版本号和VCS结帐号码?
在Ant我可以分别使用${build.number}和${build.vcs.number.1}.
谢谢.
基本上我有一个字符串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'
我有一个方法,使用递归函数输出数组的所有排列:
/// <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) 我在使用 .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 有点陌生,我无法找到断言异常按预期抛出的工作示例。有人有吗?
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)
我感到很困惑...
我最近将我的解决方案从.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) 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) 我计算了 3x3 单应矩阵,我需要获得旋转、平移、剪切和缩放以将它们用作 windows8 媒体元素属性中的参数?!
我想我几乎可以完成这项工作,但是我不知道该如何完成。
模型:
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# ×5
linq ×2
.net-4.0 ×1
.net-core ×1
autofac ×1
build ×1
c#-4.0 ×1
delegates ×1
expecto ×1
f# ×1
factory ×1
gradle ×1
hidden-field ×1
homography ×1
nunit ×1
properties ×1
razor ×1
recursion ×1
reference ×1
teamcity ×1
upgrade ×1
web-services ×1
windows-8 ×1
yield-return ×1