静态继承就像实例继承一样工作.除非您不允许将静态方法设为虚拟或抽象.
class Program {
static void Main(string[] args) {
TestBase.TargetMethod();
TestChild.TargetMethod();
TestBase.Operation();
TestChild.Operation();
}
}
class TestBase {
public static void TargetMethod() {
Console.WriteLine("Base class");
}
public static void Operation() {
TargetMethod();
}
}
class TestChild : TestBase {
public static new void TargetMethod() {
Console.WriteLine("Child class");
}
}
Run Code Online (Sandbox Code Playgroud)
这将输出:
Base class
Child class
Base class
Base class
Run Code Online (Sandbox Code Playgroud)
但我想要:
Base class
Child class
Base class
Child class
Run Code Online (Sandbox Code Playgroud)
如果我可以在静态方法上,我会使TargetMethod虚拟,它会完成这项工作.但有没有可以达到同样的效果?
编辑:是的,我可以在子类中放置一个Operation副本,但这需要复制并将大量代码粘贴到每个子节点,在我的例子中大约是35个类,这是一个维护噩梦.
我目前正在处理的项目需要在代码中输入大量的十六进制数字.
我曾经看过一张带有十六进制小键盘的旧键盘的图片(上面还有AF字母)代替了普通的小键盘.谁知道我可以在哪里获得其中一个?
我有一个嵌入了特定资源文件的编译.NET程序集(名为'Script.xml').我需要以编程方式将其更改为另一个.
如果不从源代码重新编译,这可能吗?
目前,我搜索文件中我知道的文本并且效果很好.但我需要为另一个项目做这件事,我不知道资源文件的任何内容,我需要找到另一种方法.
FileStream exe = new FileStream(currentexe, FileMode.Open);
//find xml part of exefile
string find = "<?xml version=\"1.0\"?>";
string lastchars = new string(' ', find.Length);
while (exe.CanRead) {
lastchars = lastchars.Substring(1) + (char)exe.ReadByte();
if (lastchars == find) {
exe.Seek(-find.Length, SeekOrigin.Current);
break;
}
}
//output serialized script
int bytenum = 0;
foreach (byte c in xml) {
if (c == 0) break;
exe.WriteByte(c);
bytenum++;
}
//clean out extra data
while (bytenum++ < ScriptFileSize) {
exe.WriteByte(0x20);
}
exe.Close();
Run Code Online (Sandbox Code Playgroud) 我正在开发一个位于系统托盘中的应用程序,可以在活动窗口上执行操作。但是,当单击系统托盘中的图标时,GetForegroundWindow()将返回任务栏。我需要获取任务栏之前处于活动状态的窗口。
我尝试使用EnumWindows和枚举桌面窗口GetWindow,但这通常会显示最后不活动的桌面小工具和其他顶级项目。是否有可能,或者当窗口停用时信息完全丢失?
由于我不能在组件中的元素上添加条件,因此我必须将条件分成两个单独的组件条件.从每个例子来看,这是如何做到的:
<Component Id="IIS7Webhost" Guid="482EC8D7-2DA2-48e6-A11D-6CAB3C5973E8">
<Condition><![CDATA[IIS_MAJOR_VERSION >= "#7"]]></Condition>
<CreateFolder>
<Permission User="IUSR" GenericAll="yes"/>
</CreateFolder>
</Component>
<Component Id="IIS6Webhost" Guid="51C65FAC-84B7-43d1-A230-DD9AE66B5D99">
<Condition><![CDATA[IIS_MAJOR_VERSION <= "#6"]]></Condition>
<CreateFolder>
<Permission User="IUSR_[ComputerName]" GenericAll="yes"/>
</CreateFolder>
</Component>
Run Code Online (Sandbox Code Playgroud)
但是这些组件中的两个组件都安装在每个系统上并且因为只存在其中一个用户而失败.我在这做错了什么?
IIS_MAJOR_VERSION正确设置为#6或#7.另外我相信我的语法正确,因为启动条件正常工作:
<Condition Message="Internet Information Services 5, 6, or 7 must be installed.">
<![CDATA[Installed OR (IIS_MAJOR_VERSION >= "#5" AND IIS_MAJOR_VERSION <= "#7")]]>
</Condition>
Run Code Online (Sandbox Code Playgroud)
编辑:它似乎都不安装,但Windows Installer在创建目录之前检查每个用户的存在(忽略条件).有没有办法跳过这个检查?我已经知道其中一个用户不存在.
此代码error C2504: 'IKeyEvent': base class undefined在第3行给出.
class IKeyEvent;
class EventDispatcher : private IKeyEvent {
public:
enum EEActions {
A_FEW_ACTIONS
};
private:
void OnKey(EventDispatcher::EEActions action, char multiplier);
}
class IKeyEvent {
public:
virtual void OnKey(EventDispatcher::EEActions action, char multiplier) = 0;
};
Run Code Online (Sandbox Code Playgroud)
在定义之前,您不能继承类,这是可以理解的.但是IKeyEvent直到定义之后 我才能EventDispatcher定义.
我知道我可以将其enum移出Event Dispatcher定义以使其成为全局,但这需要重构大部分程序.有没有办法EventDispatcher从依赖的类继承EventDispatcher?