我正在使用可空类型进行一些测试,并且它没有像我预期的那样工作:
int? testInt = 0;
Type nullableType = typeof(int?);
Assert.AreEqual(nullableType, testInt.GetType()); // not the same type
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
DateTime? test = new DateTime(434523452345);
Assert.IsTrue(test.GetType() == typeof(Nullable)); //FAIL
DateTime? test = new DateTime(434523452345);
Assert.IsTrue(test.GetType() == typeof(Nullable<>)); //STILL FAIL
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么testInt.GetType()返回int,而typeof(int?)返回真正的可空类型?
我想添加一个行为扩展,以便我的服务类将使用Ninject进行实例化.我创建了一个继承自BehaviorExtensionElement的类,并在我的App.config中注册它.我看不到任何明显的遗漏,但是在启动时抛出了这个:
System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element TestExtension cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.
Parameter name: element (...\MyAssembly.dll.config line 42) --->
System.ArgumentException: Extension element TestExtension cannot be added to this element.
Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.
Parameter name: element
Run Code Online (Sandbox Code Playgroud)
这是我的App.config:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="TestExtension" type="Mynamespace.DependencyInjectionServiceBehavior,MyAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<TestExtension/> …Run Code Online (Sandbox Code Playgroud) 继续对WCF服务使用依赖注入,是否有任何方法可以将DI用于WCF 验证器,以便可以执行此操作:
public class DIValidator : UserNamePasswordValidator
{
private readonly IService service;
[Inject]
public DIValidator(IService service)
{
this.service = service;
}
public override void Validate(string userName, string password)
{
service.Login(userName, password);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 我试图将Dzmitry的建议应用于我的自定义行为扩展,因为我的验证器是在app.config中定义的.遗憾的是我得到一个MethodMissingException,因为wcf希望我的验证器有一个默认的构造函数:
System.MissingMethodException: No default constructor has been defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
这是我的行为类:
public class DependencyInjectionServiceBehavior : BehaviorExtensionElement, IServiceBehavior
{
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
serviceHostBase.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = DISupport.Kernel.Get<IService>(); …Run Code Online (Sandbox Code Playgroud) Visual Studio默认将所有dll复制到每个项目的bin文件夹.这还包括系统dll'(mscorlib.dll和System.dll除外),例如System.Xml.Linq,System.ComponentModel.Composition(包括自SL4以来)等.
由于这些文件包含在每个XAP中,因此XAP大小会大幅增加.在我的有限测试中,将"Copy local"设置为false似乎并没有破坏任何东西.为什么Visual Studio将这些文件添加到bin路径?由于用户已经拥有Silverlight,我可以假设这些程序集已经安装在每个用户的GAC中,或者我错过了什么?
我开始考虑使用Diesel来查询数据库.我有一个看起来像下面结构的表(这只是一个玩具项目,可以帮助我了解Diesel的工作原理).
#[derive(Queryable, Insertable)]
#[table_name="posts"]
struct Post {
id: String,
title: String,
body: String,
published: bool
}
Run Code Online (Sandbox Code Playgroud)
例如,在编译时完成确定的查询很容易
posts.select(id, title).order(title.desc());
Run Code Online (Sandbox Code Playgroud)
我不清楚的是如何根据一些运行时参数构建查询,而不必回退到SQL.例如,JSONAPI允许动态选择字段并根据查询参数对其进行排序.我怎么在柴油机上这样做?
当我遇到这个问题时,我试图用更高种类和类型的边界来玩.我的用例是我希望能够使用Request的任何子类型或Request类型本身来参数化GenericAction实例.Action trait使用默认类型Request扩展GenericAction特征(在这种情况下,只会生成Request的匿名实例).
trait Request[+A]
trait GenericAction[A, R[_] <: Request[_]]
trait Action[A] extends GenericAction[A, Request]
trait ActionBuilderBase[R[_] <: Request[_], G[_] <: GenericAction[_,R]]
Run Code Online (Sandbox Code Playgroud)
ActionBuilderBase具有与子特征ActionBuilder和ActionBuilder2共享的实用程序方法.ActionBuilder生成默认的Action [A]和Request [A]实例.
trait ActionBuilder extends ActionBuilderBase[Request,Action]
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都那么好,但是当我尝试创建另一个使用R和GenericAction子类扩展ActionBuilderBase的特性时(在这种情况下会创建GenericAction的匿名实例),它无法编译.我想原因是在第一个ActionBuilder的情况下,请求类型已经被"填充"(因为Action [A]已经有一个请求类型== Request),这与下面的示例不同.为了让这个例子有效,我需要"填写"什么?
//Fails with "GenericAction takes two type parameters, expected: one" - what should the type annotation for GenericAction look like?
trait ActionBuilder2[R[_] <: Request[_]] extends ActionBuilderBase[R,GenericAction]
Run Code Online (Sandbox Code Playgroud) 我有一系列哈希,如下所示:
details = [
{:name => "Alice", :age => 20},
{:name => "Ted", :age => 25},
{:name => "Poppy", :age => 33},
{:name => "Amy", :age => 20},
{:name => "Ted", :age => 90},
{:name => "Amy", :age => 22},
{:name => "Ted", :age => 23}
]
Run Code Online (Sandbox Code Playgroud)
我希望能够排序,以便可以根据每个人的名字出现的次数进行排序。例如,输出可能是:
"Ted, Ted, Ted, Amy, Amy, Alice, Poppy"
谁能帮忙吗?:)
谢谢
.net ×4
wcf ×2
arrays ×1
c# ×1
copy-local ×1
gettype ×1
hash ×1
ninject ×1
nullable ×1
ruby ×1
rust ×1
rust-diesel ×1
scala ×1
silverlight ×1
wcf-security ×1