如果我想连接一个字符串N次,我更喜欢哪种方法?
以此代码为例:
public static string Repeat(this string instance, int times)
{
var result = string.Empty;
for (int i = 0; i < times; i++)
result += instance;
return result;
}
Run Code Online (Sandbox Code Playgroud)
可以在"times"设置为5或5000的情况下调用此方法.我应该使用哪种方法?
的string.join?StringBuilder的?只是标准的string.Concat?
类似的功能将在商业图书馆中实施,所以我真的需要"最佳"的方式来做到这一点.
我需要获取表达式参数的名称.我想要做的是类似于FluentNhibernate对列映射的作用:
地图(x => x.Name)
由此,我需要"名字".
我该怎么做呢?
我可以x这样做:
Expression<Func<User, object>> exp = x => x.Id;
exp.Parameters[0].Name;
Run Code Online (Sandbox Code Playgroud)
但我无法得到"名字".请注意,我没有任何可以调用的T实例.谢谢
我需要替换从我网站上的每个页面发送的一些数据,我认为使用Global.asax进行处理.这是我到目前为止尝试过的:
void Application_PreSendRequestContent(object sender, EventArgs e)
{
System.IO.StreamReader sr = new System.IO.StreamReader(Response.OutputStream);
String output = sr.ReadToEnd();
Response.ClearContent();
Response.Write("Testing..");
}
Run Code Online (Sandbox Code Playgroud)
但这给了我一个ArgumentException.我究竟做错了什么?有没有更好的方法来做到这一点?
谢谢
我想记录具有特定属性的所有类方法的所有调用.我该怎么办?
这是我到目前为止:
class ExecutionLogAttribute : Attribute
{
}
public class Human
{
private Int32 age;
[ExecutionLog]
public void HaveBirthday()
{
age++;
}
}
Run Code Online (Sandbox Code Playgroud)
什么是记录所有呼叫HaveBirthday的最佳方式?
我正在使用Phing将wordpress安装推送到我的生产服务器.是否可以在我的构建文件中定义wp-config属性,然后让phing替换wp-config内容以使用这些变量?
像这样:
<property name="prod.db_name" value="wordpress" />
<property name="prod.db_user" value="root" />
<property name="prod.db_password" value="toor" />
<property name="prod.db_host" value="prod.host.com" />
Run Code Online (Sandbox Code Playgroud)
然后,我想要一个phing任务,它接受这些值并用正确的属性替换我的wp-config.
我该怎么办?
谢谢
我正在使用C#编写一个简单的游戏,以帮助我学习基本的面向对象概念.
在下面的代码中:
class entity
{
int hp;
string name;
public entity()
{
hp = 1;
name = "entity";
}
public string status()
{
string result;
result=name + "#" + " HP:" + hp;
return result;
}
class dragon : entity
{
new public string name;
new int hp;
public dragon()
{
hp = 100;
name = "Dragon";
}
}
Run Code Online (Sandbox Code Playgroud)
我为"龙"做了一个对象
dragon mydragon = new dragon();
Run Code Online (Sandbox Code Playgroud)
问题出在以下代码中:
mydragon.status();
Run Code Online (Sandbox Code Playgroud)
这返回一个字符串,但是带有实体类对象的"name"和"hp" (即hp = 1,name = entity).
我想让它返回龙对象的值(hp = 100,name = dragon).我不确定我做错了什么,但似乎很简单. …
c# ×5
.net-3.5 ×1
asp.net ×1
attributes ×1
expression ×1
func ×1
global-asax ×1
phing ×1
php ×1