我有一个包含多个空格的字符串.我想用加号替换它们.我以为我可以用
var str = 'a b c';
var replaced = str.replace(' ', '+');
Run Code Online (Sandbox Code Playgroud)
但它只取代了第一次出现.如何让它替换所有出现的?
我想创建一个我在一个通用方法中指定的类型的实例.此类型具有许多重载的构造函数.我希望能够将参数传递给构造函数,但是
Activator.CreateInstance<T>()
Run Code Online (Sandbox Code Playgroud)
并不认为这是一个选择.
还有另一种方法吗?
我有一个代码块,将类型序列化为Html标记.
Type t = typeof(T); // I pass <T> in as a paramter, where myObj is of type T
tagBuilder.Attributes.Add("class", t.Name);
foreach (PropertyInfo prop in t.GetProperties())
{
object propValue = prop.GetValue(myObj, null);
string stringValue = propValue != null ? propValue.ToString() : String.Empty;
tagBuilder.Attributes.Add(prop.Name, stringValue);
}
Run Code Online (Sandbox Code Playgroud)
这个伟大的工程,但我希望它只是对基本类型,像这样做int
,double
,bool
等,以及其他类型的不是原始的,但可以很容易地连载一样string
.我希望它忽略列表和其他自定义类型之类的所有内容.
任何人都可以建议我这样做吗?或者我是否需要指定我想要允许的类型并打开属性的类型以查看是否允许它?这有点乱,所以如果我有一个更整洁的方式会很好.
我有以下HTML:
<div class="title">
<span>Cumulative performance</span>
<span>20/02/2011</span>
</div>
Run Code Online (Sandbox Code Playgroud)
用这个CSS:
.title
{
display: block;
border-top: 4px solid #a7a59b;
background-color: #f6e9d9;
height: 22px;
line-height: 22px;
padding: 4px 6px;
font-size: 14px;
color: #000;
margin-bottom: 13px;
clear:both;
}
Run Code Online (Sandbox Code Playgroud)
如果你检查这个jsFiddle:http: //jsfiddle.net/8JwhZ/
你可以看到名称和日期粘在一起.有没有办法可以让日期与右边对齐?我已经尝试float: right;
了第二次,<span>
但它搞砸了风格,并将日期推到了封闭的div之外
我正在尝试在我的WPF项目中使用DataAnnotations来指定字符串的最大长度,具体如下:
using System.ComponentModel.DataAnnotations;
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误
命名空间'System.ComponentModel'中不存在类型或命名空间名称'DataAnnotations'(您是否缺少程序集引用?)
我已经看到了这个命名空间DataAnnotations
中存在的其他示例.我正在使用C#4.我有什么理由不能用这个吗?我该怎么办才能修复它?
Possible Duplicate:
Why do I get “error: … must be a reference type” in my C# generic method?
I have 2 Repository methods that are almost identical:
public IList<Fund> GetFundsByName(int pageSize, string searchExpression)
{
return _session.CreateCriteria<Fund>()
.AddNameSearchCriteria<Fund>(searchExpression)
.AddOrder<Fund>(f => f.Name, Order.Asc)
.SetMaxResults(pageSize).List<Fund>();
}
public IList<Company> GetCompaniesByName(int pageSize, string searchExpression)
{
return _session.CreateCriteria<Company>()
.AddNameSearchCriteria<Company>(searchExpression)
.AddOrder<Company>(f => f.Name, Order.Asc)
.SetMaxResults(pageSize).List<Company>();
}
Run Code Online (Sandbox Code Playgroud)
The only difference is that the first one's _session.CreateCriteria
is of type Fund
and the second one is company
I was …
我听到(并在这个网站上阅读)很多关于"赞成合成而非继承".
但是什么是Compositon?我从Person:Mammal:Animal的角度理解继承,但我无法在任何地方看到Compostion的定义.有人可以填写我吗?
我有以下方法将对象序列化为HTML标记.如果类型不是Anonymous,我只想这样做.
private void MergeTypeDataToTag(object typeData)
{
if (typeData != null)
{
Type elementType = typeData.GetType();
if (/* elementType != AnonymousType */)
{
_tag.Attributes.Add("class", elementType.Name);
}
// do some more stuff
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何实现这一目标吗?
谢谢
我正在尝试实现按键功能,这将在用户点击时删除div Esc
.这适用于Firefox和IE,代码如下:
$("body").keypress(function(e) {
alert("any key pressed");
if (e.keyCode == 27) {
alert("escape pressed");
}
});
Run Code Online (Sandbox Code Playgroud)
如果我按任意键,alert
则会显示第一个键,如果我按Escape 键,则会显示第二个键alert
.
但这不适用于Chrome.alert
如果我按下任何一个字母键,则会始终显示第一个,但是当我点击Escape,Tab,Space或任何数字时,则不会显示第一个.
为什么会这样?有没有办法让Chrome响应这些按键?
我有一个页面,我正在转换为PDF.此页面包含许多段落,并不能完全适合单个页面.如果我可以减少<p>
标签之间的间距,这将有助于更多.这可能吗?谢谢.
c# ×5
css ×2
generics ×2
html ×2
reflection ×2
composition ×1
javascript ×1
jquery ×1
keypress ×1
nhibernate ×1
oop ×1
string ×1
wpf ×1