我今天读了这篇文章http://dotnetslackers.com/articles/silverlight/Silverlight-3-and-thetheWat-Form-Control-part-I.aspx关于在你拥有的Silverlight应用程序中使用MVVM模式您的域实体和视图特定实体,它们基本上是真实实体对象的子集.这不是明显违反DRY原则吗?如果是这样,你怎么能以一种很好的方式处理它呢?
我刚刚遇到这个(编写代码来演示"问题"):
public ICollection<string> CreateCollection(int x)
{
ICollection<string> collection = x == 0
? new List<string>()
: new LinkedList<string>();
return collection;
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:
Fehler CS0173:Der Typ des bedingten Ausdrucks kann nicht bestimmt werden,weil keine implizite Konvertierung zwischen"System.Collections.Generic.List"und"System.Collections.Generic.LinkedList"erfolgt.
其翻译大致为:
无法确定条件运算符的类型,因为List和LinkedList之间没有隐式转换.
我可以看到为什么编译器抱怨,但是,嘿,来吧.它试图发挥愚蠢.我可以看到两个表达式不是同一类型,而是有一个共同的祖先,作为奖励,左侧的类型也是共同的祖先.我相信编译器也可以看到它.如果左侧被声明为,我可以理解错误var.
我在这里错过了什么?
编辑:
我接受詹姆斯·冈特的解释.也许只是为了说清楚.我可以很好地阅读编译器规范.我想了解原因.为什么有人决定以这种方式编写规范.这种设计背后必然有一个原因.根据詹姆斯的说法,设计原则是"毫无意外".此外,CodeInChaos还解释了如果编译器试图从常见的祖先中推断出类型,您可能遇到的惊喜.
我有一个实现的类ICollection<SomeConcreteClass>.NUnit集合约束不会将其识别为集合.
例如Assert.That( sut, Has.No.Member( someObjectOfTypeSomeConcreteClass ) );投掷System.ArgumentException : The actual value must be a collection
并Assert.That( sut, Is.Empty );失败了sut.
那么什么时候集合是一个集合(根据NUnit)?
堆栈跟踪:
System.ArgumentException : The actual value must be a collection Parametername: actual
at NUnit.Framework.Constraints.CollectionConstraint.Matches(Object actual)
at NUnit.Framework.Constraints.NotConstraint.Matches(Object actual)
MyTestFile.cs(36,0): at MyAssembly.MyTestFixture.MyTestMethod()
Run Code Online (Sandbox Code Playgroud)
NUnit 2.4.3.0出现上述问题.我刚用2.6尝试过.Is.Empty现在有效,但Has.No.Member仍然失败.它甚至没有打电话Equals()或operator ==().它如何比较收集元素?RhinoMocks Arg<MyCollection>.List.Count( Is.Equal( 1 ) )现在也失败了.
结论:
对于NUnit 2.4,集合约束要求将集合的非泛型ICollection实现为集合(可回答原始问题).IEnumerable相等按预期工作.
使用NUnit 2.6(可能还有3.0),IEnumerable即使Equals被覆盖,匹配元素也会检查s的相等性.这就是为什么如果元素IEnumerable本身就是成员约束不起作用的原因.这是一个已知问题(https://bugs.launchpad.net/nunit-3.0/+bug/646786). …
我在 GKE 上的不同端口 (11100) 上运行节点导出器,并将 prometheus.yml 配置为使用 kubernetes_sd_configs。但是,服务发现似乎返回带有Kubelet端口 (10250)的节点 IP <node-ip>:10250/metrics。我can\xe2\x80\x99t似乎找到了一种方法来指定使用哪个端口。有任何想法吗?
- job_name: gke-nodes\n kubernetes_sd_configs:\n - role: node\nRun Code Online (Sandbox Code Playgroud)\n此外,node-exporter 在 port 中正确运行11100。我通过在内部节点 IP 中执行curl 来验证它<node-ip>:11100/metrics,它的作用就像一个魅力
这是我的节点导出器定义
\napiVersion: apps/v1\nkind: DaemonSet\nmetadata:\n name: node-exporter-ds\n namespace: monitoring\n labels:\n app: node-exporter\n belongsTo: monitoring\nspec:\n selector:\n matchLabels:\n app: node-exporter\n template:\n metadata:\n labels:\n app: node-exporter\n spec:\n serviceAccountName: monitoring-sa\n volumes:\n - name: proc\n hostPath:\n path: /proc\n - name: sys\n hostPath:\n path: /sys\n containers:\n - name: node-exporter\n image: …Run Code Online (Sandbox Code Playgroud) google-kubernetes-engine prometheus prometheus-node-exporter
在opencart网站上开发自己的支付方式(或任何模块)的信息相当稀少.是否有其他地方有关此主题的更好信息?
我有以下两个类:
class MyData {
public List<string> PropertyList { get; private set;}
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
class MyData2 {
public string PropertyA { get; set;}
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个MyClass实例,我需要将它转换为MyData2列表.我可以通过循环MyData.PropertyList并缓存其他属性值并将它们插入MyData2列表来实现,如下所示:
string propertyB = myData.PropertyB;
string propertyC = myData.PropertyC;
List<MyData2> myData2List = new List<MyData>();
foreach(string item in myData.PropertyList)
{
myData2List.Add(new myData2() { item, propertyB, propertyC });
}
Run Code Online (Sandbox Code Playgroud)
不确定是否可以使用LINQ或Lambda表达式的.Net 3.5功能完成此操作?
我想提供查询的表名作为命令参数,如下所示:
public class Foo
{
private const String myTableName = "mytable";
public void Bar()
{
NpgsqlCommand command = new NpgsqlCommand("SELECT * from :tableName", connection);
command.Parameters.Add(new NpgsqlParameter("tableName", DbType.String));
command.Parameters[0].Value = myTableName;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎导致了这个查询:"SELECT * from E'mytable'"导致错误(请注意单引号).
我真的需要为此进行字符串连接吗?从安全角度来看并不重要,因为用户不能更改表名,但创建SQL查询的字符串连接总是让我感到毛骨悚然......
谢谢,埃里克
为什么下面的LINQ to SQL语句会抛出异常?
我有一个功能
bool TrimAndCompare(string s1, string s2)
{
return customer.CustomerID.Trim() == customerID.Trim()
}
Run Code Online (Sandbox Code Playgroud)
...我在linq语句中调用上述函数的其他函数
var customers = from customer in _context.Customers
where
TrimAndCompare(customer.CustomerID, customerID)
select customer;
Run Code Online (Sandbox Code Playgroud)
上面的LINQ to SQL statment函数抛出一个异常但是下面没有为什么?
var customers = from customer in _context.Customers
where
customer.CustomerID.Trim() == customerID.Trim()
select customer;
Run Code Online (Sandbox Code Playgroud)
我得到一个'System.NotSupportedException',我尝试访问客户
我有几个包含不同 protobuf 文件的合同项目,但是某些消息类型具有相同的消息类型,例如
message user
{
Address address = 1
}
message Address
{
....
}
Run Code Online (Sandbox Code Playgroud)
我现在创建了一个共享项目并向其中添加了一个 Address.proto 文件,其中仅包含
syntax = "proto3"
option csharp_namespace = "shared.protos"
package AddressPackage
message Address {....}
Run Code Online (Sandbox Code Playgroud)
我的问题是弄清楚如何将其导入到我不同合同项目中的原型中。我已添加共享项目作为参考,但我从那里尝试的所有其他内容都导致错误。
我知道我需要使用import只是还没有弄清楚如何写字符串。
更新
我正在使用 gRPC.tools nuget 并且所有 .proto 文件都设置为 protobuf 编译器
文件结构如下
用户合同项目
两个项目都在它自己的文件夹中,并且这些文件夹彼此相邻。
在它说的共享项目中
<ItemGroup>
<None Remove="Protos\Address.proto" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\Address.proto">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Protobuf>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
并在 user.contract 中说
<ItemGroup>
<None Remove="Protos\User.proto" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\User.proto" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
提前致谢。
c# ×6
.net ×4
collections ×1
constraints ×1
dry ×1
lambda ×1
linq ×1
linq-to-sql ×1
material-ui ×1
module ×1
mvvm ×1
npgsql ×1
nunit ×1
opencart ×1
payment ×1
postgresql ×1
prometheus ×1
protobuf-net ×1
silverlight ×1
sql ×1