给定正确的"MagicName"(它类似于"CanSerialize"),以下代码将禁止空列表的xml.
这个神奇的名字是什么?
public class MyClass {
public List<int> MyList{ get; set; }
public bool MyListMagicName() { return MyList.Count != 0; }
public MyClass() { MyList = new List<int>(); }
}
Run Code Online (Sandbox Code Playgroud) 如果你有这样的话:
IBinaryAssetStructureRepository rep = new BinaryAssetStructureRepository();
var userDto = new UserDto { id = 3345 };
var dto = new BinaryAssetBranchNodeDto("name", userDto, userDto);
using (var scope1 = new TransactionScope())
{
using(var scope2 = new TransactionScope())
{
//Persist to database
rep.CreateRoot(dto, 1, false);
scope2.Complete();
}
scope1.Dispose();
}
dto = rep.GetByKey(dto.id, -1, false);
Run Code Online (Sandbox Code Playgroud)
内部TransactionScope范围2是否也会回滚?
我有一个关于RouteLink与ActionLink的问题.
请考虑以下路线
routes.MapRoute("Routename1",
"{someEnum}/SpecificAction/{id}/{stringId}",
new { controller = "MyController", id = (int?)null, stringId= (string)null, action = "SpecificAction" },
new { someEnum= "(EnumVal1|EnumVal2)" }
);
Run Code Online (Sandbox Code Playgroud)
奇怪的{someEnum}部分是因为我对枚举的所有值使用通用控制器,它构成了url的典型控制器部分.例如,/ EnumVal1/Action /和/ EnumVal2/Action /使用相同的控制器.然而,这不是问题的一部分.
考虑以下两种链接方式:
<%=Html.RouteLink("Click me","Routename1", new { id = 32, stringId = "Yatzy" })%>
<%=Html.ActionLink("Click me", "SpecificAction", "EnumVal1", new { id = 32, stringId = "Yatsy" }, null)%>
Run Code Online (Sandbox Code Playgroud)
RouteLink生成正确的URL,即/ EnumVal1/SpecificAction/32/Yatzy
ActionLink生成一个类似于/ EnumVal1/SpecificAction/32的URL?stringId = Yatzy
为什么是这样?请有人向我解释一下.
我实现了IPasswordHasher
public class MyPasswordHasher : IPasswordHasher
{
public string HashPassword(string password)
{
using (SHA256 mySHA256 = SHA256Managed.Create())
{
byte[] hash = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(password.ToString()));
StringBuilder hashSB = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
hashSB.Append(hash[i].ToString("x2"));
}
return hashSB.ToString();
}
}
public PasswordVerificationResult VerifyHashedPassword(
string hashedPassword, string providedPassword)
{
if (hashedPassword == HashPassword(providedPassword))
return PasswordVerificationResult.Success;
else
return PasswordVerificationResult.Failed;
}
}
Run Code Online (Sandbox Code Playgroud)
我在IdentityConfig中写道
manager.PasswordHasher = new MyPasswordHasher();
Run Code Online (Sandbox Code Playgroud)
但var user = await UserManager.FindAsync(model.Email, model.Password);在AccountController/Login中不要使用MyPasswordHaser.
我怎样才能在身份2.1中使用它?
在我的公司,我们有一个NuGet包,当应用于空白的Visual Studio解决方案时,将生成一个解决方案模板,其中包含一组预定义的正确命名和配置的项目.我们只需Install-package在包管理器控制台中执行常规命令即可安装软件包.
但是,这似乎已停止在Visual Studio 2015中工作.如果我尝试安装包,我会收到此错误:
install-package : Project 'Default' is not found.
At line:1 char:1
+ install-package (The package name)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Default:String) [Install-Package], ItemNotFoundException
+ FullyQualifiedErrorId : NuGetProjectNotFound,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是,这在Visual Studio 2013中运行良好.我一直在寻找有关此行为的信息,但我没有看到它提到过,也没有在任何地方报告过它.
有谁知道这里发生了什么?这是一个错误吗?是否改变了NuGet在VS 2015中的运作方式?
顺便说一句,如果重要的话,我使用的VS 2013版本是专业版,而VS 2015版则是社区版.
我正在为方法创建Traces,并希望它与自定义属性一起使用.我会用每个方法装饰TraceMethod.
例如:
[TraceMethod()]
public void SomeMethod()
{
}
public class TraceMethod : Attribute
{
public void StartTrace(){}
public void EndTrace(){}
}
Run Code Online (Sandbox Code Playgroud)
所以在这里,
如何StartTrace()在SomeMethod开始执行之前和EndTrace()执行SomeMethod结束之后调用?可能吗?
我刚刚阅读了有关浮点值比较的声明
不能使用==或!=运算符比较浮点值.大多数浮点值没有精确的二进制表示,并且精度有限.
如果是这样,比较两个浮点值的最佳方法是什么?
想知道为什么String在使用==时表现得像值类型.
String s1 = "Hello";
String s2 = "Hello";
Console.WriteLine(s1 == s2);// True(why? s1 and s2 are different)
Console.WriteLine(s1.Equals(s2));//True
StringBuilder a1 = new StringBuilder("Hi");
StringBuilder a2 = new StringBuilder("Hi");
Console.WriteLine(a1 == a2);//false
Console.WriteLine(a1.Equals(a2));//true
Run Code Online (Sandbox Code Playgroud)
StringBuilder和String的行为与==运算符不同.谢谢.
假设我们有这个模型:
public abstract class AbstractTableReferentielEntity {}
public class EstimationTauxReussite : AbstractTableReferentielEntity { }
Run Code Online (Sandbox Code Playgroud)
我为从AbstractTableReferentielEntity继承的所有类创建了一个扩展方法.
public static EntityItemViewModel ToEntityItem<T>(this T entity)
where T : AbstractTableReferentielEntity {}
Run Code Online (Sandbox Code Playgroud)
但对于一种特定类型的AbstractTableReferentielEntity(如EstimationTauxReussite),我想执行一个特定的操作,所以我创建了第二个扩展方法.
public static EntityItemViewModel ToEntityItem(this EstimationTauxReussite entity) {}
Run Code Online (Sandbox Code Playgroud)
所有扩展方法都在同一名称空间中声明.
之后,我从具有Entity Framework的数据库中检索一些数据:
protected List<EntityItemViewModel> GetAllActifEntityItem<T>()
where T : AbstractTableReferentielEntity
{
return Context
.Set<T>()
.Where(item => item.IsActif)
.Select(item => item.ToEntityItem())
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
它汇编.
当运行时的T是EstimationTauxReussite类型时,它ToEntityItem在我调用时会进入错误的方法Select(item => item.ToEntityItem()).它没有进入最具体的扩展方法.有任何想法吗 ?
在正常情况下,以下代码每两分钟很好地记录:
Observable.Interval(TimeSpan.FromSeconds(120)).Subscribe(
l =>
{
var now = DateTime.Now;
Console.WriteLine(
"Tick at " + now.ToString("hh:mm:ss") + " Count " + l.ToString());
});
Run Code Online (Sandbox Code Playgroud)
Tick at 11:30:00 Count 0
(11:31:55 put computer to sleep)
(no tick at 11:32:00 because PC is suspended, which is fine)
(no tick at 11:34:00 because PC is suspended)
(11:34:30 wake computer up)
(no tick as soon as possible to indicate that ticks were missed)
Tick at 11:36:30 Count 1
Run Code Online (Sandbox Code Playgroud)
似乎Observable Interval定时器类重置自身并从设备恢复时起重新开始间隔.(我观察到与Observable Timer类似的行为)
我需要一个不会在恢复时重启间隔的计时器,但如果在睡眠期间错过了任何刻度,则需要一次计时器
c# ×9
.net ×6
asp.net-mvc ×2
actionlink ×1
asp.net ×1
comparison ×1
inheritance ×1
nuget ×1
routes ×1
suspend ×1
timer ×1