给定以下结构:
public struct Foo<T>
{
public Foo(T obj) { }
public static implicit operator Foo<T>(T input)
{
return new Foo<T>(input);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码编译:
private Foo<ICloneable> MakeFoo()
{
string c = "hello";
return c; // Success: string is ICloneable, ICloneable implicitly converted to Foo<ICloneable>
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有编译 - 为什么?
private Foo<ICloneable> MakeFoo()
{
ICloneable c = "hello";
return c; // Error: ICloneable can't be converted to Foo<ICloneable>. WTH?
}
Run Code Online (Sandbox Code Playgroud) 不知道如何调用它,但是说你有一个类似下面的类:
class Person
{
public string Name;
public IEnumerable<Person> Friends;
}
Run Code Online (Sandbox Code Playgroud)
然后你有一个人,你想要递归地"展开"这个结构,所以你最终得到一个没有重复的所有人的列表.
你会怎么做?我已经做了一些似乎有用的东西,但我很想知道其他人会怎么做,特别是如果Linq内置了一些内容你可以巧妙地使用它来解决这个小问题:)
这是我的解决方案:
public static IEnumerable<T> SelectRecursive<T>(this IEnumerable<T> subjects, Func<T, IEnumerable<T>> selector)
{
// Stop if subjects are null or empty
if(subjects == null)
yield break;
// For each subject
foreach(var subject in subjects)
{
// Yield it
yield return subject;
// Then yield all its decendants
foreach (var decendant in SelectRecursive(selector(subject), selector))
yield return decendant;
}
}
Run Code Online (Sandbox Code Playgroud)
将使用这样的东西:
var people = somePerson.SelectRecursive(x => x.Friends);
Run Code Online (Sandbox Code Playgroud) 我使用的是VSTS 2008 + C#+ .Net 2.0.当执行以下语句时,会从String.Format语句中抛出FormatException,任何想法有什么问题?
这是获取我正在使用的template.html的地方.我想在template.html中格式化此部分m = {0}.
string template = String.Empty;
using (StreamReader textFile = new StreamReader("template.html"))
{
template = textFile.ReadToEnd();
String.Format(template, "video.wmv");
}
Run Code Online (Sandbox Code Playgroud)
http://www.mediafire.com/download.php?u4myvhbmmzg
编辑1:
这是我的template.html的内容,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
<title>Silverlight Project Test Page </title>
<style type="text/css">
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
}
</style>
<script type="text/javascript">
function onSilverlightError(sender, args) { …Run Code Online (Sandbox Code Playgroud) 为什么静态虚拟不可能?C#依赖还是在OO世界中没有任何意义?
我知道这个概念已被强调,但我没有找到上一个问题的简单答案.
一旦设置了初始值,我就有几个不可变的类.Eric Lippert称之为一次性写入不可变性.
在C#中实现一次写入不变性通常意味着通过构造函数设置初始值.这些值初始化只读字段.
但是,如果需要使用XmlSerializer或DataContractSerializer将类似这样的类序列化为XML,则必须具有无参数构造函数.
有没有人建议如何解决这个问题?是否有其他形式的不变性可以更好地与序列化?
编辑:正如@Todd所指出的,DataContractSerializer不需要无参数构造函数.根据MSDN上的DataContractSerializer文档,DataContractSerializer"不会调用目标对象的构造函数."
我有一个使用asp.net成员资格模式的网站.我想在aspnet_users表上设置一个触发器,它将user_id和新行的user_name插入到另一个表中.
如何从最后一次插入中获取值?
我可以选择最后一个date_created,但这看起来很臭.有没有更好的办法?
如果我有一个泛型类:
public class MyClass<T>
{
public T Value;
}
Run Code Online (Sandbox Code Playgroud)
我想实例化几个项目,如...
new MyClass<string>
new MyClass<int>
Run Code Online (Sandbox Code Playgroud)
...并将它们添加到集合中.如何定义集合以便它可以包含泛型类型列表?然后我想在某个时刻迭代集合,并使用Value属性.可能?
我无法读到我试过的那些特殊字符
第一种方式#
string xmlFile = File.ReadAllText(fileName);
Run Code Online (Sandbox Code Playgroud)
第二种方式#
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string s = r.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
但是这两个陈述都不理解那些特殊字符.我该怎么读?
更新###
我也尝试所有编码
string xmlFile = File.ReadAllText(fileName, Encoding. );
Run Code Online (Sandbox Code Playgroud)
但仍然不明白那些特殊的人物.
我想完全关闭它,而不是获得任何解决方案范围的提示.我只看到我可以暂停的地方,但找不到关闭它的答案.
c# ×8
.net ×2
generics ×2
file ×1
ienumerable ×1
immutability ×1
methods ×1
recursion ×1
resharper ×1
sql ×1
sql-server ×1
static ×1
streamwriter ×1
triggers ×1
virtual ×1
winforms ×1