我想在运行时动态地向ExpandoObject添加属性.所以例如添加一个字符串属性调用NewProp我想写类似的东西
var x = new ExpandoObject();
x.AddProperty("NewProp", System.String);
Run Code Online (Sandbox Code Playgroud)
这很容易吗?
这个问题让我想知道泛型方法的具体实现在哪里实际存在.我试过谷歌,但我没有想出正确的搜索.
如果我们采用这个简单的例子:
class Program
{
public static T GetDefault<T>()
{
return default(T);
}
static void Main(string[] args)
{
int i = GetDefault<int>();
double d = GetDefault<double>();
string s = GetDefault<string>();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的脑海中,我总是假设在某些时候它会导致实现3个必要的具体实现,这样,在天真的伪修改中,我们将有这个逻辑的具体实现,其中使用的特定类型导致正确的堆栈分配等.
class Program
{
static void Main(string[] args)
{
int i = GetDefaultSystemInt32();
double d = GetDefaultSystemFloat64();
string s = GetDefaultSystemString();
}
static int GetDefaultSystemInt32()
{
int i = 0;
return i;
}
static double GetDefaultSystemFloat64()
{
double d = 0.0;
return d;
}
static string GetDefaultSystemString() …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有任何方法可以检查对象是否特别是JavaScript中的日期.isType返回Date的对象,这对于此方案是不够的.有任何想法吗?谢谢!
我正在使用它System.Net.Http.HttpClient
做一些客户端HTTP通信.我在一个地方得到了所有的HTTP,从其余的代码中抽象出来.在一个实例中,我想将响应内容作为流读取,但是流的使用者与HTTP通信发生的位置以及流被打开的情况很好地隔离.在负责HTTP通信的地方我处理所有的HttpClient
东西.
此单元测试将失败Assert.IsTrue(stream.CanRead)
:
[TestMethod]
public async Task DebugStreamedContent()
{
Stream stream = null; // in real life the consumer of the stream is far away
var client = new HttpClient();
client.BaseAddress = new Uri("https://www.google.com/", UriKind.Absolute);
using (var request = new HttpRequestMessage(HttpMethod.Get, "/"))
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
//here I would return the stream to the caller
stream = await response.Content.ReadAsStreamAsync();
}
Assert.IsTrue(stream.CanRead); // FAIL if response is disposed so is the stream …
Run Code Online (Sandbox Code Playgroud) 我试图在WPF数据网格中显示查询的结果.我绑定的ItemsSource类型是IEnumerable<dynamic>
.由于返回的字段直到运行时才确定,因此在评估查询之前我不知道数据的类型.每个"行"都返回为ExpandoObject
具有表示字段的动态属性.
我希望AutoGenerateColumns
(如下所示)能够从ExpandoObject
静态类型生成列,但它似乎不会.
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Results}"/>
Run Code Online (Sandbox Code Playgroud)
无论如何要以声明方式执行此操作,还是必须使用某些C#进行必要的操作?
编辑
好的,这会给我正确的列:
// ExpandoObject implements IDictionary<string,object>
IEnumerable<IDictionary<string, object>> rows = dataGrid1.ItemsSource.OfType<IDictionary<string, object>>();
IEnumerable<string> columns = rows.SelectMany(d => d.Keys).Distinct(StringComparer.OrdinalIgnoreCase);
foreach (string s in columns)
dataGrid1.Columns.Add(new DataGridTextColumn { Header = s });
Run Code Online (Sandbox Code Playgroud)
所以现在只需要弄清楚如何将列绑定到IDictionary值.
为什么C#需要运算符重载是静态方法而不是成员函数(如C++)?(也许更具体地说:这个决定的设计动机是什么?)
ParallelEnumerable
有一个静态成员AsParallel
.如果我有一个IEnumerable<T>
并且想要使用Parallel.ForEach
那意味着我应该一直使用AsParallel
吗?
例如,这些都是正确的(其他一切都是平等的)吗?
没有AsParallel
:
List<string> list = new List<string>();
Parallel.ForEach<string>(GetFileList().Where(file => reader.Match(file)), f => list.Add(f));
Run Code Online (Sandbox Code Playgroud)
还是用AsParallel
?
List<string> list = new List<string>();
Parallel.ForEach<string>(GetFileList().Where(file => reader.Match(file)).AsParallel(), f => list.Add(f));
Run Code Online (Sandbox Code Playgroud) 我可以回想一下,在使用MFC时,您可以通过检查_MFC_VER
宏来支持多个版本的MFC框架.
我现在正在使用.NET 4做一些事情,并希望在几个地方使用Tuple,但仍然保持其他一切3.5兼容.
我想做的事情如下:
#if DOTNET4
public Tuple<TSource, TResult> SomeMethod<TSource, TResult>(){...}
#else
public KeyValuePair<TSource, TResult> SomeMethod<TSource, TResult>(){...}
#endif
Run Code Online (Sandbox Code Playgroud)