我有以下类层次结构:
public class Row : ICloneable, IComparable, IEquatable<Row>,
IStringIndexable, IDictionary<string, string>,
ICollection<KeyValuePair<string, string>>,
IEnumerable<KeyValuePair<string, string>>,
System.Collections.IEnumerable
{ }
public class SpecificRow : Row, IXmlSerializable,
System.Collections.IEnumerable
{
public void Add(KeyValuePair<MyEnum, string> item) { }
}
Run Code Online (Sandbox Code Playgroud)
但是,尝试执行以下操作会出错:
var result = new SpecificRow
{
{MyEnum.Value, ""},
{MyEnum.OtherValue, ""}
};
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
集合初始化程序的最佳重载Add方法'Row.Add(string,string)'具有一些无效参数
我怎样才能使得在派生类上使用对象初始化器SpecificRow允许类型MyEnum?好像应该看到这个Add方法SpecificRow.
更新: 我实现了一个额外的接口,SpecificRow所以它现在看起来像这样:
public class SpecificRow : Row, IXmlSerializable,
System.Collections.IEnumerable,
ICollection<KeyValuePair<MyEnum, string>>
{ }
Run Code Online (Sandbox Code Playgroud)
但是,我仍然得到同样的Add错误.我将尝试IDictionary<MyEnum, string>下一步.
我在Jag Reeghal的博客上阅读了这篇文章.在我看来,他所建议的内容与使用对象初始化程序实际上并不相同.然后我意识到我并不确定.
构造一个对象时,使用对象初始化器,并且其中一个初始化器抛出(可能是Null Reference异常)......实际构造的对象是什么?这基本上就像是在构造函数中抛出的异常吗?或者对象是否完全构造,然后初始化?
假设我在javascript中定义了以下内容:
com.company.long.namespace = {
actions: {
add: {
defaults: {
url: 'myurl/submit',
},
invoke: function () {
var submitUrl = this.defaults.url;
com.company.long.namespace.foo.util.server.submit({
url: submitUrl,
success: function() { }
});
},
}
}
};
Run Code Online (Sandbox Code Playgroud)
然后我在JQuery click事件的上下文中调用它:
$(myElem).click(function() {
com.company.long.namespace.actions.add.invoke();
});
Run Code Online (Sandbox Code Playgroud)
由于'this'在jQuery事件回调中的作用方式,因此从此上下文调用时,this.defaults是未定义的.无论如何仍然在这个范围内使用'this',而不必定义完整的命名空间,或者不使用jQuery.proxy?
我有一个自定义Matrix类,我想实现一个自定义对象初始化程序,类似于double [,]可以使用,但似乎无法弄清楚如何实现它.
理想情况下,我希望看起来像这样
var m1 = new Matrix
{
{ 1.0, 3.0, 5.0 },
{ 7.0, 1.0, 5.0 }
};
Run Code Online (Sandbox Code Playgroud)
截至目前,我有一个带有签名的常规构造函数
public Matrix(double[,] inputArray){...}
Run Code Online (Sandbox Code Playgroud)
接受这样的电话
var m1 = new Matrix(new double[,]
{
{ 1.0, 3.0, 5.0 },
{ 7.0, 1.0, 5.0 }
});
Run Code Online (Sandbox Code Playgroud)
和一个对象初始化程序,通过继承IEnumerable<double[]>接口和实现公共void Add(double[] doubleVector)方法来接受以下使用
var m2 = new Matrix
{
new [] { 1.0, 3.0, 5.0 },
new [] { 7.0, 1.0, 5.0 }
};
Run Code Online (Sandbox Code Playgroud)
当我尝试使用对象初始化器时,我想得到一个没有重载的编译器错误Add,需要X个参数,其中X是我试图创建的列数(即在我提供的示例3中).
如何设置我的课程来接受我提供的论证?
这是一个完整简单的工作示例
import multiprocessing as mp
import time
import random
class Foo:
def __init__(self):
# some expensive set up function in the real code
self.x = 2
print('initializing')
def run(self, y):
time.sleep(random.random() / 10.)
return self.x + y
def f(y):
foo = Foo()
return foo.run(y)
def main():
pool = mp.Pool(4)
for result in pool.map(f, range(10)):
print(result)
pool.close()
pool.join()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我怎样才能修改它,所以Foo只被每个工人初始化一次,而不是每个任务?基本上我想要初始化调用4次,而不是10次.我使用的是python 3.5
python pool object-initializers python-multiprocessing python-3.5
是否可以使用对象初始化器在一行中执行以下操作(例如初始化bool数组并将所有元素设置为true)?
int weeks = 5;
bool[] weekSelected = new bool[weeks];
for (int i = 0; i < weeks; i++)
{
weekSelected[i] = true;
}
Run Code Online (Sandbox Code Playgroud)
我无法让它发挥作用.
编辑:我应该提到我使用VS2008与.NET 2.0(所以Enumerable将无法正常工作).
假设您有一个Price对象接受(int数量,小数价格)或包含"4/$ 3.99"的字符串.有没有办法限制哪些属性可以设置在一起?请在下面的逻辑中随意纠正我.
测试:A和B彼此相等,但不允许使用C示例.因此,问题如何强制所有三个参数都不会像在C示例中那样被调用?
AdPrice A = new AdPrice { priceText = "4/$3.99"}; // Valid
AdPrice B = new AdPrice { qty = 4, price = 3.99m}; // Valid
AdPrice C = new AdPrice { qty = 4, priceText = "2/$1.99", price = 3.99m};// Not
Run Code Online (Sandbox Code Playgroud)
班级:
public class AdPrice {
private int _qty;
private decimal _price;
private string _priceText;
Run Code Online (Sandbox Code Playgroud)
构造函数:
public AdPrice () : this( qty: 0, price: 0.0m) {} // Default Constructor
public AdPrice (int qty = 0, decimal …Run Code Online (Sandbox Code Playgroud) 我正在使用一个linq查询,看起来(经过一些简化)后面的内容如下:
List<UserExams> listUserExams = GetUserExams();
var examData =
from userExam in listUserExams
group by userExam.ExamID into groupExams
select new ExamData()
{
ExamID = groupExams.Key,
AverageGrade = groupExams.Average(e => e.Grade),
PassedUsersNum = groupExams.Count(e => /* Some long and complicated calculation */),
CompletionRate = 100 * groupExams.Count(e => /* The same long and complicated calculation */) / TotalUsersNum
};
Run Code Online (Sandbox Code Playgroud)
困扰我的是对PassedUsersNum和CompletionRate两次出现的计算表达式.
假设CompletionRate = (PassedUsersNum / TotalUsersNum) * 100,如何通过重用PassedUsersNum的计算来编写它,而不是再次编写该表达式?
我有:
var result = _client.Search<ElasticFilm>(new SearchRequest("blaindex", "blatype")
{
From = 0,
Size = 100,
Query = titleQuery || pdfQuery,
Source = new SourceFilter
{
Include = new []
{
Property.Path<ElasticFilm>(p => p.Url),
Property.Path<ElasticFilm>(p => p.Title),
Property.Path<ElasticFilm>(p => p.Language),
Property.Path<ElasticFilm>(p => p.Details),
Property.Path<ElasticFilm>(p => p.Id)
}
},
Timeout = "20000"
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试添加一个荧光笔过滤器,但我不太熟悉对象初始值设定项 (OIS) C# 语法。我已经检查了NEST 官方页面和 SO,但似乎无法针对(OIS)返回任何结果。
我可以在 Nest.SearchRequest 类中看到 Highlight 属性,但我没有足够的经验(我猜)从那里简单地构建我需要的东西 - 关于如何使用带有 OIS的荧光笔的一些示例和解释会很热门!
I'm wondering if the use of object initializers inside using statements somehow prevents the correct disposal of the resource declared inside them, e.g.
using (Disposable resource = new Disposable() { Property = property })
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
I've read that object initializers are nothing but synctatic sugar, which the compiler translates to something similar to the following code:
MyClass tmp = new MyClass();
tmp.Property1 = 1;
tmp.Property2 = 2;
actualObjectYouWantToInitialize = tmp;
Run Code Online (Sandbox Code Playgroud)
Even if I may appear as a …
c# ×8
.net-2.0 ×1
c#-4.0 ×1
closures ×1
dictionary ×1
inheritance ×1
javascript ×1
jquery ×1
linq ×1
nest ×1
polymorphism ×1
pool ×1
python ×1
python-3.5 ×1