当你有一个名为与类型相同的局部变量时,有没有办法告诉编译器你给出的符号是一个类型还是一个变量?例如考虑(并忽略所有类型的返回错误):
public class sometype { public static sometype DoSomething() {} }
public string sometype { get { return sometype.DoSomething(); } } //A
public string sometype { get { return sometype.Trim(); } } //B
public sometype sometype { get { return sometype.DoSomething(); } } //C
public sometype sometype { get { return sometype.Trim(); } } //D
Run Code Online (Sandbox Code Playgroud)
(如果XSD让你烦恼,你可能想跳过这个):
我目前正在努力让LINQ到XSD工作.在我的XSD文件中有xs:这样的元素:
<xs:element name="car" type="car">
Run Code Online (Sandbox Code Playgroud)
'car'类型被定义为这样的simpleType
(可能还有一些限制,但这本质上是它):
<xs:simpleType name="car">
<xs:restriction base="xs:string" /> …Run Code Online (Sandbox Code Playgroud) 我觉得我已经非常接近于解决这个问题并阅读了数十篇文章和现有问题.
我有这样的方法:
public T DoStuff(object arg1, object arg2)
{
/* Do stuff and return a T */
}
Run Code Online (Sandbox Code Playgroud)
我需要能够将此方法传递给另一个类以进行回调.
但是,当这个其他类回调时,它将使用仅在运行时知道的不同的预期返回类型.
例如:
public void SomeMethod(MethodInfo m, ref object caller)
{
MethodInfo callback = m.MakeGenericMethod(new Type[] { runtimeType });
callback.Invoke(caller, new [] { arg1val, arg2val });
}
/* DoStuff() will make a call to SomeMethod() passing
* itself (the method and the object) */
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够做到这一点
我找到了适合这些方法的方法,但没有一方能够涵盖所有这些方法.
可能吗?
我想知道是否有可能生成一个有效期为(大约)三个月的"密钥"?
例如,让我们说(假设)我生成一个像这样的密钥(伪代码):
Key = HASH ( MachineID, Salt );
Run Code Online (Sandbox Code Playgroud)
我验证密钥有效的方法是这样检查:
isValid(Key)
{
return Key == HASH ( MachineID, Salt )
}
Run Code Online (Sandbox Code Playgroud)
你会如何扩展它来生成这样的键:
Key = HASH ( MachineID, Salt, LastMonth, ThisMonth, NextMonth );
Run Code Online (Sandbox Code Playgroud)
但仍然有你的isValid正常工作?
我能看到的一种方法是:
isValid(Key)
{
return Key == HASH ( MachineID, Salt, (LastMonth), (ThisMonth), (NextMonth) )
|| Key == HASH ( MachineID, Salt, (LastMonth-1), (LastMonth), (ThisMonth) )
|| Key == HASH ( MachineID, Salt, (ThisMonth), (ThisMonth+1), (ThisMonth+2) )
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有任何更好的想法.
我有一个同事在一个有"数量"列的桌子上工作.他们希望在同一查询中获得前5个金额和金额总和.
我知道你可以这样做:
SELECT TOP 5 amount FROM table
UNION SELECT SUM(amount) FROM table
ORDER BY amount DESC
Run Code Online (Sandbox Code Playgroud)
但这会产生如下结果:
1000 (sum)
100
70
50
30
20
Run Code Online (Sandbox Code Playgroud)
他们真正需要的是:
100 | 1000
70 | 1000
50 | 1000
30 | 1000
20 | 1000
Run Code Online (Sandbox Code Playgroud)
我实现这一目标的直观尝试往往会遇到分组问题,当您选择不同的列时,这不是一个问题,而是当您想要根据您选择的列使用聚合函数时.
c# ×2
.net ×1
delegates ×1
encryption ×1
generics ×1
hash ×1
key ×1
linq-to-xsd ×1
reflection ×1
security ×1
sql ×1
sql-server ×1