我在字符串中有一个相对或绝对的url.我首先需要知道它是绝对的还是相对的.我该怎么做呢?然后我想确定url的域是否在允许列表中.
这是我的允许列表,作为一个例子:
string[] Allowed =
{
"google.com",
"yahoo.com",
"espn.com"
}
Run Code Online (Sandbox Code Playgroud)
一旦我知道它的相对或绝对,我认为它相当简单:
if (Url.IsAbsolute)
{
if (!Url.Contains("://"))
Url = "http://" + Url;
return Allowed.Contains(new Uri(Url).Host);
}
else //Is Relative
{
return true;
}
Run Code Online (Sandbox Code Playgroud) 我在TStringList类中使用分隔符时遇到问题.看一看:
var
s: string;
sl: TStringList;
begin
sl := TStringList.Create;
s := 'Users^foo bar^bar foo^foobar^barfoo';
sl.Delimiter := '^';
sl.DelimitedText := s;
ShowMessage(sl[1]);
end;
Run Code Online (Sandbox Code Playgroud)
sl[1] 应该回来 'foo bar'
sl[1] 回来了 'foo'
看来分隔符现在是'^'AND' '
有任何想法吗?
我正在寻找最便携的方法来检查MS SQL Server中是否存在触发器.它至少需要处理SQL Server 2000,2005,最好是2008年.
信息似乎不在INFORMATION_SCHEMA中,但如果它在某处,我宁愿从那里使用它.
我知道这个方法:
if exists (
select * from dbo.sysobjects
where name = 'MyTrigger'
and OBJECTPROPERTY(id, 'IsTrigger') = 1
)
begin
end
Run Code Online (Sandbox Code Playgroud)
但我不确定它是否适用于所有SQL Server版本.
如何在WPF(C#或VB.Net)中找到应用程序可执行文件的位置?
我在Windows窗体中使用了这段代码:
Application.ExecutablePath.ToString();
Run Code Online (Sandbox Code Playgroud)
但是使用WPF我从Visual Studio收到此错误:
System.Window.Application不包含ExecutablePath的定义.
我正在使用此代码阻止我的程序的第二个实例同时运行,是否安全?
Mutex appSingleton = new System.Threading.Mutex(false, "MyAppSingleInstnceMutx");
if (appSingleton.WaitOne(0, false)) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
appSingleton.Close();
} else {
MessageBox.Show("Sorry, only one instance of MyApp is allowed.");
}
Run Code Online (Sandbox Code Playgroud)
我担心如果有什么东西抛出一个异常并且应用程序崩溃了Mutex仍然会被保留.真的吗?
这真的是两个问题,但是它们非常相似,为了保持简单,我想我只是把它们放在一起:
首先:鉴于已经建立的python项目,除了简单的代码内优化之外,有什么方法可以加快速度?
其次:在python中从头开始编写程序时,有哪些好方法可以大大提高性能?
对于第一个问题,想象一下你得到了一个写得很好的项目,你需要提高性能,但你似乎无法通过重构/优化获得很多收益.在这种情况下你会做些什么来加速它,而不是像C那样重写它?
我有一个这样的课:
private class MyClass {
[DisplayName("Foo/Bar")]
public string FooBar { get; private set; }
public string Baz { get; private set; }
public bool Enabled;
}
Run Code Online (Sandbox Code Playgroud)
当我创建一个List<MyClass>并将其分配给DataSourcea时DataGridView,网格显示两列,"Foo/Bar"和"Baz".这就是我想要发生的事情.
它目前有效,因为Enabled是一个字段,而不是属性 - DataGridView只会获取属性.但是,这是一个肮脏的黑客.
我也想使Enabled成为一个属性,但仍然将它隐藏在DataGridView上.
我知道我可以在绑定后手动删除列..但这并不理想.
是否有类似于DisplayName的属性,我可以用?标记属性?有点像[Visible(false)]?
我有一个React应用程序,其中父组件的道具传递给子组件,然后道具设置子项的状态.
将更新后的值发送到父组件后,子组件不会使用更新的props更新状态.
如何让它更新子组件的状态?
我的精简代码:
class Parent extends React.Component {
constructor (props) {
super(props);
this.state = {name: ''}
}
componentDidMount () {
this.setState({name: this.props.data.name});
}
handleUpdate (updatedName) {
this.setState({name: updatedName});
}
render () {
return <Child name={this.state.name} onUpdate={this.handleUpdate.bind(this)} />
}
}
class Child extends React.Component {
constructor (props) {
super(props);
this.state = {name: ''}
}
componentDidMount () {
this.setState({name: this.props.name});
}
handleChange (e) {
this.setState({[e.target.name]: e.target.value});
}
handleUpdate () {
// ajax call that updates database with updated name and then …Run Code Online (Sandbox Code Playgroud) 我需要一些简单的字符串加密,所以我写了下面的代码(从这里有很多"灵感" ):
// create and initialize a crypto algorithm
private static SymmetricAlgorithm getAlgorithm(string password) {
SymmetricAlgorithm algorithm = Rijndael.Create();
Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(
password, new byte[] {
0x53,0x6f,0x64,0x69,0x75,0x6d,0x20, // salty goodness
0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65
}
);
algorithm.Padding = PaddingMode.ISO10126;
algorithm.Key = rdb.GetBytes(32);
algorithm.IV = rdb.GetBytes(16);
return algorithm;
}
/*
* encryptString
* provides simple encryption of a string, with a given password
*/
public static string encryptString(string clearText, string password) {
SymmetricAlgorithm algorithm = getAlgorithm(password);
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText); …Run Code Online (Sandbox Code Playgroud) 我想使用javascript为文档注册keypress事件.
我用过:
document.attachEvent("onkeydown", my_onkeydown_handler);
Run Code Online (Sandbox Code Playgroud)
它适用于IE,但不适用于Firefox和Chrome.
我也尝试过:
document.addEventListener("onkeydown", my_onkeydown_handler, true);
// (with false value also)
Run Code Online (Sandbox Code Playgroud)
但它仍然无法与Firefox和Chrome一起使用.
有解决方案吗,我错过了什么吗?
c# ×5
javascript ×2
winforms ×2
.net ×1
attributes ×1
datagridview ×1
delimiter ×1
delphi ×1
delphi-7 ×1
encryption ×1
events ×1
exception ×1
mutex ×1
optimization ×1
parsing ×1
performance ×1
properties ×1
python ×1
reactjs ×1
sql-server ×1
triggers ×1
tstringlist ×1
url ×1
vb.net ×1
wpf ×1