是否Ninject有类似于Unity父/子容器的概念来提供基本的继承模型?我用谷歌搜索但没有找到任何东西.
inheritance dependency-injection ninject ioc-container unity-container
我正在尝试对返回SelectList的函数进行单元测试.
如何从SelectList中检索项目以验证我的模型是否正确构造?
mySelectList.Items.First().DataValue
管他呢.
我花了一些时间在我的代码中找到这个bug.尝试创建临时测试目录:
Path.Combine("C:", "test");
Directory.CreateDirectory(path);
Run Code Online (Sandbox Code Playgroud)
所以这不会创建目录C:\test.它似乎只是test在执行程序集的本地文件夹中创建目录.
这是因为Path.Combine("C:", "test");退货C:test,而不是C:\test.
我不会假装对文件系统了解太多,所以也许这个原因应该是显而易见的,但我不明白.我不确定他们为什么这样实现Path,或者为什么CreateDirectory不抱怨无效的目录名称.
我正在尝试每小时创建一个新的日志文件,并在服务器上运行以下代码.正在创建当天的第一个日志文件并写入正常,但当天没有创建更多日志文件.什么想法可能会出错?也没有抛出异常.
private void LogMessage(Message msg)
{
string name = _logDirectory + DateTime.Today.ToString("yyyyMMddHH") + ".txt";
using (StreamWriter sw = File.AppendText(name))
{
sw.WriteLine(msg.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 我对以下如何进行必要的演员表示感到有点困惑:
public IList<IMyClass> Foo()
{
IList<IMyClass> foo = SomeQuery();
var result = foo.GroupBy(x => x.bar).Select(x => new MyClass()).ToList();
// So now I have a List<MyClass> which needs casting as IList<IMyClass>
return result;
}
Run Code Online (Sandbox Code Playgroud)
使用显式转换在运行时失败.错误消息没有用,但我猜它可能与GroupBy有关,因为我通常不希望这样的事情有任何问题.
请参阅这里的 jsFiddle示例
我申请padding-top到的li尝试对齐文本更接近底部.但它只是做得li更大,尽管似乎有足够的空间来适应文本.
有任何想法吗?
<ul>
<li class="padded">x</li>
<li>x</li>
</ul>?
li {
width: 25px;
height: 25px;
border: solid 1px black;
display: inline;
margin: 0 2px 0 0;
float: left;
}
.padded {
padding: 3px 0 0 0;
text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
我在IE7和Chrome中得到了相同的结果,没有检查任何其他浏览器.
我需要从Web App访问远程驱动器.ASP.NET进程无法访问该驱动器,因此我想模拟当前用户的请求.
我看到了一些使用WindowsImpersonationContext的基本示例,并尝试了以下内容.
WindowsImpersonationContext impersonationContext = null;
try
{
impersonationContext = ((WindowsIdentity)User.Identity).Impersonate();
file.SaveAs(filePath);
}
finally
{
if (impersonationContext != null)
{
impersonationContext.Undo();
}
}
Run Code Online (Sandbox Code Playgroud)
我仍然得到访问被拒绝的例外.
我已经阅读了一些相关内容LogonUser,但据我所知,您需要一个用户名和密码才能从中获取凭据.我不打算以不同的特定用户身份登录,只是当前用户(谁应该有权访问文件存储),所以我认为我实际上并不需要LogonUserAPI.
有谁知道我错过了什么?上述代码应该有效吗?
我还要注意包括
<identity impersonate="true" />
不起作用,但包括
<identity impersonate="true" userName="myName" password="myPassword" />
在web.config在让我,我见过几个人问这是为什么的问题,但我见过的任何解释.我想知道它是否与我的问题有关.
我试图用LINQ 将项目附加List<string>到a中StringBuilder:
items.Select(i => sb.Append(i + ","));
Run Code Online (Sandbox Code Playgroud)
我发现了一个类似的问题在这里这解释了为何上述不工作,但我无法找到Each的ForEach或任何类似的List,我可以改用.
有没有一种巧妙的方法在一个班轮中这样做?
class ExpenseClaim
{
int EmployeeId;
int Team;
double Cost;
}
List<EmployeeExpense> GetEmployeeExpenses()
{
return _expenseClaims // `_expenseClaims` is `List<ExpenseClaim>`
.GroupBy(e => e.EmployeeId)
.Select(x =>
new EmployeeExpense(
x.Key,
// TODO: employee team?
x.Sum(e => e.Cost)
);
}
Run Code Online (Sandbox Code Playgroud)
请原谅相当人为的例子.
如何让员工团队进入GetEmployeeExpenses?我假设我需要第二组,但我无法解决语法问题.
请注意,对于给定的员工,Team无论如何他们总是相同,所以我很乐意采用Team例如第一个按记录分组的.
所以...
ExpenseClaim { EmployeeId = 1, Team = Sales, Cost = 100 }
ExpenseClaim { EmployeeId = 1, Team = Sales, Cost = 50 }
=>
EmployeeExpense { EmployeeId = 1, Team = Sales, Cost …Run Code Online (Sandbox Code Playgroud) public class SimpleLogger
{
static readonly string logFile = ConfigurationManager.AppSettings["LogFile"];
static StreamWriter GetStream()
{
return File.Exists(logFile) ?
File.AppendText(logFile) : File.CreateText(logFile);
}
public static void Write(string msg)
{
using (var sw = GetStream())
{
sw.Write(msg);
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码无法使用,因为它似乎没有正确关闭/处理流.后续写入会给出"正在使用的文件"IOException.
如果将类修改为使用非静态方法,则它似乎正常工作.
我不明白为什么会有任何行为差异?
c# ×7
.net ×5
group-by ×2
linq ×2
c#-4.0 ×1
casting ×1
css ×1
directory ×1
disposable ×1
filepath ×1
filestream ×1
html ×1
ienumerable ×1
inheritance ×1
interface ×1
iterator ×1
list ×1
logging ×1
ninject ×1
padding ×1
path ×1
security ×1
selectlist ×1
static ×1
stream ×1
streamwriter ×1