当在编译时未知类型参数但是在运行时动态获取时,调用泛型方法的最佳方法是什么?
考虑以下示例代码 - 在Example()
方法内部,GenericMethod<T>()
使用Type
存储在myType
变量中调用的最简洁方法是什么?
public class Sample
{
public void Example(string typeName)
{
Type myType = FindType(typeName);
// What goes here to call GenericMethod<T>()?
GenericMethod<myType>(); // This doesn't work
// What changes to call StaticMethod<T>()?
Sample.StaticMethod<myType>(); // This also doesn't work
}
public void GenericMethod<T>()
{
// ...
}
public static void StaticMethod<T>()
{
//...
}
}
Run Code Online (Sandbox Code Playgroud) 我总是使用(a)Nullable<>.HasValue
因为我喜欢语义.然而,最近我正在研究其他人现有的代码库,他们Nullable<> != null
专门使用(b)代替.是否有理由使用一个而不是另一个,还是纯粹的偏好?
(一个)
int? a;
if (a.HasValue)
// ...
Run Code Online (Sandbox Code Playgroud)
(b)中
int? b;
if (b != null)
// ...
Run Code Online (Sandbox Code Playgroud) 我的项目中有一个派生类的泛型类.
public class GenericClass<T> : GenericInterface<T>
{
}
public class Test : GenericClass<SomeType>
{
}
Run Code Online (Sandbox Code Playgroud)
有没有办法找出一个Type
对象是否来自GenericClass
?
t.IsSubclassOf(typeof(GenericClass<>))
Run Code Online (Sandbox Code Playgroud)
不起作用.
想象一下代码:
public class obj
{
// elided
}
public static Dictionary<string, obj> dict = new Dictionary<string, obj>();
Run Code Online (Sandbox Code Playgroud)
方法1
public static obj FromDict1(string name)
{
if (dict.ContainsKey(name))
{
return dict[name];
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
方法2
public static obj FromDict2(string name)
{
try
{
return dict[name];
}
catch (KeyNotFoundException)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我很好奇这两个函数的性能是否存在差异,因为第一个函数应该比第二个函数更低 - 假设它需要在字典包含值时检查两次,而第二个函数确实只需要访问字典曾经但是WOW,它实际上是相反的:
循环1 000 000个值(现有10万个,不存在90 000个):
第一个功能:306毫秒
第二功能:20483毫秒
这是为什么?
编辑:你可以在下面这个问题的评论中注意到,如果有0个非现有密钥,第二个函数的性能实际上略好于第一个函数.但是,一旦存在至少一个或多个非现有密钥,则第二个密钥的性能会迅速下降.
我有一个活动,它是整个应用程序中使用的主要活动,它有许多变量.我有两个其他活动,我希望能够使用第一个活动的数据.现在我知道我可以这样做:
GlobalState gs = (GlobalState) getApplication();
String s = gs.getTestMe();
Run Code Online (Sandbox Code Playgroud)
但是我想分享很多变量,有些变量可能比较大,所以我不想像上面那样创建它们的副本.
有没有办法直接获取和更改变量而不使用get和set方法?我记得在Google开发网站上阅读了一篇文章,称不建议在Android上使用此功能.
在C#7中我们可以使用
if (x is null) return;
Run Code Online (Sandbox Code Playgroud)
代替
if (x == null) return;
Run Code Online (Sandbox Code Playgroud)
使用新方法(前一个例子)比旧语法有什么好处吗?
语义学有什么不同?
只是品味问题?如果没有,何时使用一个或另一个.
参考.
是什么区别int
,System.Int16
,System.Int32
和System.Int64
其他比自己的尺寸?
在Visual Studio 2015中调试C#应用程序时,诊断工具会自动启动.我取消选中了" 选择工具"中的两个复选框,但它似乎并未完全禁用它.
如何将其关闭(以后再打开)?
这里简单的好奇心,带着一些实际问题,因为我偶尔会被这个问题所困扰.
我经常看到人们使用Where.FirstOrDefault()
搜索并抓住第一个元素.为什么不用Find()
?对方是否有优势?我无法区分.
namespace LinqFindVsWhere
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.AddRange(new string[]
{
"item1",
"item2",
"item3",
"item4"
});
string item2 = list.Find(x => x == "item2");
Console.WriteLine(item2 == null ? "not found" : "found");
string item3 = list.Where(x => x == "item3").FirstOrDefault();
Console.WriteLine(item3 == null ? "not found" : "found");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
generics ×2
null ×2
reflection ×2
android ×1
c#-7.0 ×1
dictionary ×1
java ×1
linq ×1
nullable ×1
performance ×1
sharing ×1