是否可以使用匿名类型实现接口.我有一段我想要工作的代码,但不知道该怎么做.
我有几个答案要么说不,要么创建一个实现接口的类构造新的实例.这不是很理想,但我想知道是否有一种机制可以在界面上创建一个瘦动态类,这将使这个变得简单.
public interface DummyInterface
{
string A { get; }
string B { get; }
}
public class DummySource
{
public string A { get; set; }
public string C { get; set; }
public string D { get; set; }
}
public class Test
{
public void WillThisWork()
{
var source = new DummySource[0];
var values = from value in source
select new
{
A = value.A,
B = value.C + "_" + value.D
};
DoSomethingWithDummyInterface(values);
}
public …Run Code Online (Sandbox Code Playgroud) 编辑:我已将结果写成博客文章.
C#编译器有点神奇地处理COM类型.例如,这个陈述看起来很正常......
Word.Application app = new Word.Application();
Run Code Online (Sandbox Code Playgroud)
......直到你意识到这Application是一个界面.在接口上调用构造函数?Yoiks!这实际上被转换为对Type.GetTypeFromCLSID()另一个的调用Activator.CreateInstance.
此外,在C#4中,您可以对ref参数使用非ref 参数,并且编译器只是添加一个局部变量以通过引用传递,丢弃结果:
// FileName parameter is *really* a ref parameter
app.ActiveDocument.SaveAs(FileName: "test.doc");
Run Code Online (Sandbox Code Playgroud)
(是的,有一堆参数丢失.不是可选参数好吗?:)
我正在尝试调查编译器的行为,我没有假装第一部分.我可以做第二部分没有问题:
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
[ComImport, GuidAttribute("00012345-0000-0000-0000-000000000011")]
public interface Dummy
{
void Foo(ref int x);
}
class Test
{
static void Main()
{
Dummy dummy = null;
dummy.Foo(10);
}
}
Run Code Online (Sandbox Code Playgroud)
我想能够写:
Dummy dummy = new Dummy();
Run Code Online (Sandbox Code Playgroud)
虽然.显然它会在执行时爆炸,但没关系.我只是在试验.
编译器为链接的COM PIA(CompilerGenerated和TypeIdentifier)添加的其他属性似乎没有做到这一点......什么是神奇的酱油?
假设你有一个人类:
public class Person
{
public string Name { get; set;}
public IEnumerable<Role> Roles {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我显然应该在构造函数中实例化角色.现在,我曾经使用像这样的List:
public Person()
{
Roles = new List<Role>();
}
Run Code Online (Sandbox Code Playgroud)
但我在System.Linq命名空间中发现了这种静态方法
IEnumerable<T> Enumerable.Empty<T>();
Run Code Online (Sandbox Code Playgroud)
来自MSDN:
该
Empty(TResult)()方法缓存一个空的序列TResult.当它返回的对象被枚举时,它不会产生任何元素.在某些情况下,此方法对于将空序列传递给用户定义的方法非常有用
IEnumerable(T).它还可以用于为诸如的方法生成中性元素Union.有关此用法的示例,请参阅示例部分
那么编写这样的构造函数会更好吗?你用它吗?为什么?如果没有,为什么不呢?
public Person()
{
Roles = Enumerable.Empty<Role>();
}
Run Code Online (Sandbox Code Playgroud) 在C#中,我使用LINQ和IEnumerable一点点.一切都很好(或至少大部分都是如此).
但是,在很多情况下,我发现自己需要一个空IEnumerable<X>的默认值.也就是说,我想
for (var x in xs) { ... }
Run Code Online (Sandbox Code Playgroud)
无需空检查即可工作.现在这是我目前所做的,取决于更大的背景:
var xs = f() ?? new X[0]; // when xs is assigned, sometimes
for (var x in xs ?? new X[0]) { ... } // inline, sometimes
Run Code Online (Sandbox Code Playgroud)
现在,虽然上面对我来说完全没问题 - 也就是说,如果创建数组对象有任何"额外开销"我只是不在乎 - 我想知道:
在C#/ .NET中是否存在"空的不可变IEnumerable/IList"单例?(而且,即使没有,是否有一种"更好"的方式来处理上述情况?)
Java具有Collections.EMPTY_LIST不可变的单例 - "良好类型"的通道Collections.emptyList<T>()- 用于此目的,虽然我不确定类似的概念是否甚至可以在C#中工作,因为泛型的处理方式不同.
谢谢.
我发现允许修改值类型中的迭代器方法this.
但是,由于CLR的限制,调用方法看不到修改.(this按值传递)
因此,迭代器和非迭代器中的相同代码会产生不同的结果:
static void Main() {
Mutable m1 = new Mutable();
m1.MutateWrong().ToArray(); //Force the iterator to execute
Console.WriteLine("After MutateWrong(): " + m1.Value);
Console.WriteLine();
Mutable m2 = new Mutable();
m2.MutateRight();
Console.WriteLine("After MutateRight(): " + m2.Value);
}
struct Mutable {
public int Value;
public IEnumerable<int> MutateWrong() {
Value = 7;
Console.WriteLine("Inside MutateWrong(): " + Value);
yield break;
}
public IEnumerable<int> MutateRight() {
Value = 7;
Console.WriteLine("Inside MutateRight(): " + Value);
return new int[0];
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Inside …
object selectedDataItem;
MyClass.Inventory inventory;
inventory = (MyClass.Inventory) selectedDataItem;
Run Code Online (Sandbox Code Playgroud)
在inventory我们可以看到的细节如下:
Trace.Writeline(inventory.Name + " " + inventory.Place);
Run Code Online (Sandbox Code Playgroud)
你看库存有inventory.Name,Inventory.Place我想包装IEnumerable或ObservableCollection中的所有属性,这样我就可以一次遍历所有库存,而不是通过inventory.Name,inventory.Place等等...
如何制作库存IEnumerable以便我可以这样做:
IEnumerable<MyClass.Inventory> enumerable = (IEnumerable<MyClass.Inventory>) inventory;
enumerable = from x in enumerable where x.Name == inventory.Name select x;
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做,错误是
无法将类型为"MyClass.Inventory"的对象强制转换为"System.Collections.Generic.IEnumerable`1 [MyClass.Inventory]".
c# ×6
ienumerable ×2
.net ×1
c#-4.0 ×1
casting ×1
com ×1
constructor ×1
empty-list ×1
iterator ×1
linq ×1
singleton ×1
this ×1
value-type ×1