泛型类中的静态字段将为每个泛型参数组合具有单独的值.因此它可以用作Dictionary <Type,无论 >
这是不是一个静态的字典更好或更坏<类型,无论 >?
换句话说,哪些实现更有效?
public static class MethodGen<TParam> {
public static readonly Action<TParam> Method = CreateMethod();
static Action<TParam> CreateMethod() { /*...*/ }
}
Run Code Online (Sandbox Code Playgroud)
要么,
public static class MethodGen {
static readonly Dictionary<Type, Delegate> methods
= new Dictionary<Type, Delegate>();
public static Action<T> GetMethod<T>() {
//In production code, this would ReaderWriterLock
Delegate method;
if(!methods.TryGetValue(typeof(T), out method)
methods.Add(typeof(t), method = CreateMethod<T>());
return method;
}
static Action<T> CreateMethod<T>() { /*...*/ }
}
Run Code Online (Sandbox Code Playgroud)
特别是,CLR如何通过泛型类型参数查找静态字段?
我需要在.Net WinForms应用程序中存储单个密码的哈希值.
什么是最安全的方法?
特别是:
我假设算法应该是SHA512或HMACSHA512.
我正在用C#编写一个在后台运行的程序,并允许用户使用热键在活动窗口中切换键盘布局.(Windows仅支持CTRL+ SHIFT和ALT+ SHIFT)
我正在使用RegisterHotKey来捕获热键,它运行正常.
问题是我找不到任何API来改变聚焦窗口的键盘布局.
ActivateKeyboardLayout和LoadKeyboardLayout只能更改调用线程的键盘布局.
有谁知道如何更改不同线程的键盘布局(语言栏的方式)?
我有一个像这样的Generic类:
public class Repository<T> {...}
Run Code Online (Sandbox Code Playgroud)
我需要用字符串来实例...示例:
string _sample = "TypeRepository";
var _rep = new Repository<sample>();
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?这甚至可能吗?
谢谢!
这是我的代码的一部分
这个
<%= Html.ActionLink(Model[x].Title, "Index", "q", new { slug = Model[x].TitleSlug, id = Model[x].PostID }, null)%>
Run Code Online (Sandbox Code Playgroud)
产生这个网址
http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4
Run Code Online (Sandbox Code Playgroud)
但我想生成一个带有片段的url,如下所示:
http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4#1
Run Code Online (Sandbox Code Playgroud)
有没有办法使用HTML.ActionLink函数执行此操作?
我有一个名为LabelX1的标签.这是在form2上.在form1上,我有一个按钮.我希望将按钮的文本传输到另一个表单的标签.我试过了
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
Run Code Online (Sandbox Code Playgroud)
但它不起作用.这样做有简单直接的方法吗?
考虑这个例子:
public final class Main<T extends Main<T>> {
public static void main(String[] args) {
Main<?> main = new Main<>();
}
}
Run Code Online (Sandbox Code Playgroud)
这完全编译.但是当我尝试在不使用菱形的情况下进行编译时,我能够使用它的唯一方法是使用原始类型.
Main<?> main = new Main();
Run Code Online (Sandbox Code Playgroud)
没有原始类型的尝试不起作用:
Main<?> main = new Main<?>(); // None of
Main<?> main = new Main<Main<?>>(); // these
Main<?> main = new Main<Main<Main<?>>>(); // compile
Run Code Online (Sandbox Code Playgroud)
那么为什么钻石的原始版本有效呢?你写的时推断类型是什么Main<?> main = new Main<>();?
它是推断原始类型,还是推断某种无限嵌套类型?
Main<Main<Main<Main<...>>>>
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×5
generics ×3
asp.net ×1
asp.net-mvc ×1
clr ×1
covariance ×1
dictionary ×1
dns ×1
hash ×1
iterator ×1
java ×1
networking ×1
passwords ×1
reflection ×1
security ×1
vb.net ×1
winapi ×1
winforms ×1