我在帖子中看到了以下函数,它允许用户使用通用表达式对数据进行排序:
public static IOrderedQueryable<T> OrderBy<T, TKey>(
this IQueryable<T> source, Expression<Func<T, TKey>> func, bool isDescending) {
return isDescending ? source.OrderByDescending(func) : source.OrderBy(func);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用这个函数时,我得到一个错误,说"找不到类型或命名空间名称"TKey'(你是否错过了使用指令或汇编引用?)".我在这里做了一些愚蠢的事情,但我可以弄明白了.
编辑:
在做了一些研究之后,我认为我的问题在于构建我传递给它的Expression.是否可以构建一个可以包含不同类型的表达式?假设我的数据集有一个字符串,一个int和一个bool,我想使用上面的泛型函数来排序任何项目.我该怎么做呢?
我现在有这个工作:
if (IsString)
{
Expression<Func<T, string>> expString = ...;
// call orderBy with expString
}
else if (IsInt)
{
Expression<Func<T, int>> expInt;
// call orderBy w/ expInt
}
:
Run Code Online (Sandbox Code Playgroud)
我想要的东西:
Expression<Func<T, {something generic!}>> exp;
if (IsString)
exp = ...;
else if (IsInt)
exp = ...;
:
// call orderBy with exp
Run Code Online (Sandbox Code Playgroud) 我正在使用基于gcc的工具链在嵌入式处理器上开发ac应用程序。在我的应用程序中,我需要在特定的内存位置放置一个标志。我需要链接器命令文件语法的帮助来完成此任务。
我的老板刚刚安装了 Windows 7,他尝试运行我们的安装程序之一,该安装程序在 XP 下运行得非常好。在 Windows 7 上,安装程序运行时不会出现任何错误。但是,它不会在 HKEY_LOCAL_MACHINE\SOFTWARE{Company}{product} 下创建注册表项。这些密钥在 XP 下可以正确创建。
有人遇到过这个问题吗?我怀疑这是一个权限/安全问题,但我不确定,而且我没有 Windows 7 可供试验。
编辑
相关计算机是运行 64 位 Windows 的 64 位计算机。事实证明,Windows 7 将 32 位应用程序重定向到 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node{Company}{product}。问题是我的应用程序代码尝试使用如下硬编码值访问注册表:
var t = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\..., "ValueName", DefaultValue);
Run Code Online (Sandbox Code Playgroud)
那么,我的新问题是如何访问注册表以使 Windows 9 注册表重定向正常工作?
registry redirect visual-studio-2005 setup-deployment windows-7
假设我有一些类定义如下:
class Security
{
Boolean AuthenticateUser(String username, String password);
Boolean AddUser(String username, String password);
// many more methods
}
class NetworkedDevice
{
void Stop();
void Start();
// many more methods
}
Run Code Online (Sandbox Code Playgroud)
然后我有另一个包含上述类的实例的类.如何避免以下代码?我希望通过这个类公开class1和class2的所有方法.
class MyWindowsService
{
Security _security = new Security();
NetworkDevice _netDevice = new NetworkDevice();
Boolean AuthenticateUser(String username, String password)
{
return _security.AuthenticateUser(username, password);
}
// all the rest of "Security" methods implemented here
void StopNetworkDevice()
{
_netDevice.Stop();
}
void StartNetorkDevice()
{
_netDevice.Start();
}
// all the rest of "NetDevice" …Run Code Online (Sandbox Code Playgroud)