我正在尝试从包含通用对象的列表中删除重复的条目.
public class MessageInfo
{
public DateTime Date { get; set; }
public string To { get; set; }
public string Message { get; set; }
}
public class SMSDupeRemover : IEqualityComparer<MessageInfo>
{
public bool Equals(MessageInfo x, MessageInfo y)
{
throw new NotImplementedException();
}
public int GetHashCode(MessageInfo obj)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
以及删除欺骗的代码:
IEnumerable<MessageInfo> new_texts = text_messages.Distinct(new SMSDupeRemover());
Run Code Online (Sandbox Code Playgroud)
问题是Equals和从不调用GetHashCode.任何人都知道为什么?
出于学习目的,我正在编写自己的Active Directory客户端.为了执行解锁帐户和重置密码等命令,我必须输入管理员帐户和密码,并使用并WindowsIdentity.Impersonate运行代码作为我的管理员帐户.我想知道我保护密码的选项,因为它必须在我的程序中多次使用.
据我所知,我可以SecureString用来存储密码.但是为了运行代码作为我的管理员帐户,我已经使用了WindowsIdentity.Impersonate,并且要使用我必须得到一个令牌,LogonUser从中需要常规string而不是SecureString.
所以我必须:
在启动时登录
将输入转换为 SecureString
清除输入
然后,当我想要执行需要提升的函数时:
将先前创建的转换SecureString为string
将转换后的字符串传递给 LogonUser
清除转换字符串 null
执行命令
清除LogonUser对象
这是接近这个的正确方法吗?这似乎不可思议必须有转换的SecureString要string使用它...好像它会破坏的目的,并留下密码更容易,而我将它转换.
编辑:修复了LogonUser的名称
任何想法为什么使用var velocity = ...给我一个未声明的错误?当我使用int velocity = ...它时没有错误.
// Error: Cannot use local variable 'velocity' before it is declared
var velocity = int.TryParse(txtVelocity.Text, out velocity) ? velocity : -1;
// Valid
int velocity = int.TryParse(txtVelocity.Text, out velocity) ? velocity : -1;
Run Code Online (Sandbox Code Playgroud) 我有一个名为ExecuteCommand的函数,它根据用户的输入执行操作.这些事情的范围可以从简单地做一个Console.Writeline(),检查我的表单上的复选框,或模拟键盘到另一个进程,完全独立于我自己.该函数在单独的线程上运行,因此更改UI将需要一些调用.我有两种方法可以做到这一点......其中一种我不确定是一种好方法,但它很容易.
下面的代码,第3行是我有一个问题:
private void ExecuteCommand()
{
this.Invoke((MethodInvoker)delegate()
{
if (current_line_index < command_que.Count)
{
current_line = command_que[current_line_index];
if (current_line.StartsWith(">>Auto Enter"))
{
chkAutoEnter.Checked = false;
}
else if (current_line.StartsWith("+WinWait("))
{
string title_to_wait_for = current_line;
title_to_wait_for = title_to_wait_for.Remove(0, "+WinWait(\"".Length);
title_to_wait_for = title_to_wait_for.Remove(title_to_wait_for.Length - 2, 2);
t_WinWait = new Thread(() => WinWait(title_to_wait_for));
t_WinWait.Name = "WinWait";
t_WinWait.Start();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
代码完美无缺......但我不确定这是不是很好的做法.
或者,我知道我可以做这样的事情来改变UI:
private delegate void CheckCheckBoxHandler(bool checked);
private void CheckCheckBox(bool checked)
{
if (this.chkAutoEnter.InvokeRequired)
{
this.chkAutoEnter.Invoke(new CheckCheckBoxHandler(this.CheckCheckBox), checked);
}
else
{
chkAutoEnter.Checked = checked; …Run Code Online (Sandbox Code Playgroud) 使用此作为一个例子,我是能够得到下拉列表中的工作.不幸的是,我无法弄清楚如何确定从C#后端检查哪些复选框.我尝试更换<option>标签,<asp:CheckBox...但是下拉按钮右侧的复选框显示在一起.我也试过下面的代码:
<select class="multiselect" multiple="multiple">
<option value="cheese" id="chkCheese" runat="server">Cheese</option>
<option value="tomatoes">Tomatoes</option>
...
</select>
Run Code Online (Sandbox Code Playgroud)
我现在可以chkCheese使用上面的代码从CS文件访问,但是没有.Checked或的属性.Selected.知道为什么吗?