我有一个template.txt包含以下内容的文件:
Hello ${something}
Run Code Online (Sandbox Code Playgroud)
我想创建一个PowerShell脚本来读取文件并扩展模板中的变量,即
$something = "World"
$template = Get-Content template.txt
# replace $something in template file with current value
# of variable in script -> get Hello World
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
我有一个包含类的c ++头文件.我想在几个项目中使用这个类,我不想为它创建一个单独的库,所以我将两个方法声明和定义放在头文件中:
// example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
namespace test_ns{
class TestClass{
public:
void testMethod();
};
void TestClass::testMethod(){
// some code here...
}
} // end namespace test_ns
#endif
Run Code Online (Sandbox Code Playgroud)
如果在同一个项目中我从多个cpp文件中包含这个头文件,我会收到一条错误说" multiple definition of test_ns::TestClass::testMethod()",而如果我将方法定义放在类体中,则不会发生这种情况:
// example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
namespace test_ns{
class TestClass{
public:
void testMethod(){
// some code here...
}
};
} // end namespace test_ns
#endif
Run Code Online (Sandbox Code Playgroud)
由于类是在命名空间内定义的,这两种形式不应该是等价的吗?为什么在第一种情况下认为方法定义了两次?
我有一个python脚本,它分析一组错误消息,并检查每条消息是否与特定模式(正则表达式)匹配,以便对这些消息进行分组.例如,"文件x不存在"和"文件y不存在"将匹配"文件.*不存在"并且被视为"未找到文件"类别的两次出现.
随着模式和类别的数量不断增加,我想把这些夫妇"正则表达/显示字符串"放在配置文件中,基本上是某种类型的字典序列化.
我希望这个文件可以手工编辑,所以我放弃任何形式的二进制序列化,而且我宁愿不采用xml序列化来避免字符转义问题(&<>等等...... ).
你知道什么是实现这个目标的好方法吗?
更新:感谢Daren Thomas和Federico Ramponi,但我不能拥有可能任意代码的外部python文件.
我需要在Active Directory中创建一个新用户.我找到了几个例子如下:
using System;
using System.DirectoryServices;
namespace test {
class Program {
static void Main(string[] args) {
try {
string path = "LDAP://OU=x,DC=y,DC=com";
string username = "johndoe";
using (DirectoryEntry ou = new DirectoryEntry(path)) {
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Add(username);
ou.CommitChanges();
}
}
catch (Exception exc) {
Console.WriteLine(exc.Message);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我没有错误,但没有创建新用户.
我正在运行测试的帐户具有足够的权限来在目标组织单位中创建用户.
我错过了什么(可能是用户对象的一些必需属性)?
任何想法为什么代码不给出例外?
编辑
以下为我工作:
int NORMAL_ACCOUNT = 0x200;
int PWD_NOTREQD = 0x20;
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Value = username;
user.Properties["userAccountControl"].Value = …Run Code Online (Sandbox Code Playgroud) 如果在switch语句中抛出异常,我注意到异常堆栈跟踪中的行号有奇怪的行为.
这是一个例子(格式当然是因为行号):
using System;
class Program {
static void Main(string[] args) {
for (int i = 0; i < 3; i++) {
try {
ThrowSomeException(i);
} catch (Exception exc) {
Console.WriteLine(exc);
}
}
}
private static void ThrowSomeException(int arg) {
Console.WriteLine("arg = {0}", arg);
switch (arg) {
case 0:
throw new Exception("Line number = 16");
case 1:
throw new Exception("Line number = 18");
default:
throw new Exception("Line number = 20");
}
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪中报告的行是switch语句中下一个异常的行.上面的程序产生了这个结果(注意异常文本中的行号与堆栈跟踪中的行号不匹配):
arg = …Run Code Online (Sandbox Code Playgroud) 使用T4代码生成,是否可以访问当前项目中定义的类型?
例如,如果我有一个接口,我想将其实现委托给另一个类,即
interface IDoSomething {
public void do_something();
}
class DoSomethingImpl : IDoSomething {
public void do_something() {
// implementation...
}
}
class SomeClass : IDoSomething {
IDoSomething m_doSomething = new DoSomethingImpl();
// forward calls to impl object
public void do_something() {
m_doSomething.do_something();
}
}
Run Code Online (Sandbox Code Playgroud)
我想SomeClass用代码生成自动化呼叫转发; 这可能吗?
在PowerShell脚本中,我有一些传递给Format-TableCmdLet的对象.
我的脚本输出如下所示:
Something...
Operation AttributeName AttributeValue
--------- ------------- --------------
Delete Member John Doe
Something else...
Run Code Online (Sandbox Code Playgroud)
由于字段的含义非常明显,我想从Format-Table的输出中删除标题,'---'分隔符和开头和结尾的空行.
我不认为CmdLet支持这个(或者至少如果有一个参数来做这个我找不到它).
将Format-Table的输出中只包含实际值的行的最佳方法是什么?
是否有可能使用Bash获得以某个字符串开头的命令列表?
我想在输入命令的开头后得到打印<tab>两次的内容,例如,将其存储在变量中.
在Visual Studio宏中,如何在输出窗格(即通常包含构建输出的窗口)上编写执行信息?
我正在使用Visual Studio 2008,如果这是相关的.
解决方案:我在我的宏项目中添加了以下子项,我将它们发布在这里,以防它们有用.
Private Sub Write(ByVal name As String, ByVal message As String)
Dim output As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim window As OutputWindow = output.Object
Dim pane As OutputWindowPane = window.OutputWindowPanes.Item(name)
pane.Activate()
pane.OutputString(message)
pane.OutputString(Environment.NewLine)
End Sub
Private Sub Log(ByVal message As String, ByVal ParamArray args() As Object)
Write("Debug", String.Format(message, args))
End Sub
Private Sub Log(ByVal message As String)
Write("Debug", message)
End Sub
Run Code Online (Sandbox Code Playgroud) 从PowerShell脚本,我如何确定脚本是否已经过点源,即它已被调用
. .\myscript.ps1
Run Code Online (Sandbox Code Playgroud)
而不是
.\myscript.ps1
Run Code Online (Sandbox Code Playgroud)
注意一个有趣的博客文章(也)关于这个:http://poshoholic.com/2008/03/18/powershell-deep-dive-using-myinvocation-and-invoke-expression-to-support-dot-sourcing-and -direct-调用功能于共享的powershell的脚本/
.net ×3
c# ×3
powershell ×3
bash ×1
c++ ×1
exception ×1
formatting ×1
linux ×1
macros ×1
python ×1
scripting ×1
stack-trace ×1
t4 ×1
variables ×1