我有我的抽象基类A:
public abstract class A : ICloneable {
public int Min { get; protected set; }
public int Max { get; protected set; }
public A(int low, int high)
{
this.Min = low;
this.Max = high;
}
//...
public object Clone()
{
return new this(this.Min, this.Max); //<-- ??
}
}
Run Code Online (Sandbox Code Playgroud)
我的B级扩展了哪个:
public class B : A
{
public B(int low, int high) : base(low, high) { }
//...
}
Run Code Online (Sandbox Code Playgroud)
由于A是抽象的,因此无法实例化,但派生类可以.是否可以从A类创建B类的新实例? …
假设我有以下字典:
private Dictionary<string, IEnumerable<string>> dic = new Dictionary<string, IEnumerable<string>>();
//.....
dic.Add("abc", new string[] { "1", "2", "3" });
dic.Add("def", new string[] { "-", "!", ")" });
Run Code Online (Sandbox Code Playgroud)
如何获得IEnumerable<Tuple<string, string>>
包含以下组合的内容:
{
{ "abc", "1" },
{ "abc", "2" },
{ "abc", "3" },
{ "def", "-" },
{ "def", "!" },
{ "def", ")" }
}
Run Code Online (Sandbox Code Playgroud)
它并不必须是一个Tuple<string, string>
,但它似乎是更appropiate类型.
我正在寻找一个简单的LINQ解决方案,如果有的话.
我尝试过以下方法:
var comb = dic.Select(i => i.Value.Select(v => Tuple.Create<string, string>(i.Key, v)));
Run Code Online (Sandbox Code Playgroud)
但comb
最终成为了类型IEnumerable<IEnumerable<Tuple<string, string>>>
.
假设我有以下任务:
var task = _entityManager.UseRepositoryAsync(async (repo) =>
{
IEnumerable<Entity> found = //... Get from repository
return new
{
Data = found.ToList()
};
}
Run Code Online (Sandbox Code Playgroud)
的类型是task
什么?
实际上,结果是:System.Threading.Tasks.Task<'a>
,
'a
匿名类型在哪里:{ List<object> Data }
如何在不使用的情况下明确说明这种类型var
?
我已经尝试过Task<a'> task = ...
,Task<object> task = ...
但无法对其进行编译。
我有一个方法 ( UseApplicationCache<T>
),它将 aFunc<Task<T>>
作为参数。
我还有一个cache
用户可能设置为true
or的变量false
。
如果true
,则应调用上述方法并将 mytask
作为参数传递,如果false
,则应执行 mytask
而不将其作为方法的参数。
我的最终结果是这样的:
Func<Task<?>> fetch = () …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
IEnumerable<int> elements = Enumerable.Range(1, Int32.MaxValue);
Console.WriteLine("Size of System.Int32: {0}", sizeof(int));
Console.Write("Iterating over {0} elements... ", elements.Count());
Parallel.ForEach(elements, _ => { });
Console.WriteLine("Done.");
Run Code Online (Sandbox Code Playgroud)
打印出:
> Size of System.Int32: 4
> Iterating over 2147483647 elements... Done.
Run Code Online (Sandbox Code Playgroud)
但是,我不明白为什么这不扔OutOfMemoryException
.
知道每个int
值占用4
空间字节,分配Int32.MaxValue
量int
应占用~8GB
检查我的申请,这个过程正在采取行动.~5.200KB.
元素正在成功迭代,因此必须在某处分配,对吧?
LINQ如何实现这一目标?
考虑以下对象:
Controller controller = new Controller()
{
Name = "Test",
Actions = new Action[]
{
new Action() { Name = "Action1", HttpCache = 300 },
new Action() { Name = "Action2", HttpCache = 200 },
new Action() { Name = "Action3", HttpCache = 400 }
}
};
Run Code Online (Sandbox Code Playgroud)
如何将此对象映射到以下表单的字典?
#key# -> #value#
"Test.Action1" -> 300
"Test.Action2" -> 200
"Test.Action3" -> 400
Run Code Online (Sandbox Code Playgroud)
那就是一个Dictionary<string, int>
.
我对LINQ解决方案感兴趣,但我无法解决它.
我试图将每个动作映射到KeyValuePair,但我不知道如何获取每个动作的父控制器的Name属性.
IDictionary<TKey, TValue>
从接口扩展ICollection<KeyValuePair<TKey, TValue>>
,所以它有一个Add(KeyValuePair<TKey, TValue> item)
方法:
IDictionary<string, object> dic = new Dictionary<string, object>();
dic.Add(new KeyValuePair<string, object>("number", 42)); // Compiles fine
Run Code Online (Sandbox Code Playgroud)
但是,尽管是Dictionary<TKey, Tvalue>
implements IDictionary<TKey, TValue>
,它没有这个方法重载:
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add(new KeyValuePair<string, object>("number", 42)); // Does not compile
Run Code Online (Sandbox Code Playgroud)
怎么会这样?
我们假设我有以下课程:
public class Person {
public string Name { get; set; }
public string Surname { get; set; }
public string FullName {
get {
return Name + " " + Surname;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下块:
Person person = new Person();
person.Name = "Matt";
person.Surname = "Smith";
return person.FullName;
Run Code Online (Sandbox Code Playgroud)
会回来的Matt Smith
.
让我们将Person
类型更改为动态ExpandoObject
.
代码如下所示:
dynamic person = new ExpandoObject();
person.Name = "Matt";
person.Surname = "Smith";
Run Code Online (Sandbox Code Playgroud)
但这就是我被困的地方.如何覆盖get
新FullName
属性的访问者?
我可以实现创建新方法的相同效果:
person.GetFullName = (Func<string>)(() => …
Run Code Online (Sandbox Code Playgroud) 假设我有这个课程:
public class StreamEntity : IPersistableEntity, IDisposable
{
public Stream Stream { get; set; }
public void Dispose()
{
if(Stream != null) Stream.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
现在假设我想将这个类的实例存储在某种缓存中,比如MemoryCache.
在将实体存储在缓存中之后,我继续进行操作.
一段时间后,消费者试图检索同一个实体,所以我从缓存中获取它,但是有一点问题......对象被丢弃了,所以我无法读取它的Stream
属性(还记得我们在缓存之后处理它吗?)
现在,解决这个问题的一种方法是永远不要处理缓存的实体.这似乎是合乎逻辑的,因为它们应该在内存中保持活着,但在到期后会发生什么?
具体来说,当MemoryCache
自动删除已过期的对象时,它会将其丢弃吗?
根据Will MemoryCache在被驱逐时处理IDisposable项目?,MemoryCache
不会处置其缓存的项目.
知道了这一点,我该如何缓存一次性物体?如果我无法控制他们的驱逐,我应该何时处置这些实体?
我正在使用Bootstrap 3.3.5,我有两列:.col-md-9
和.col-md-3
.
但是,具有该类的那个.col-md-3
以某种方式占据父行的全宽,而不是仅占用25%.
为什么会这样?
注意:在整页中运行代码段并调整浏览器窗口的大小,直到您看到此行为.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-9">
<div>Element</div>
<div>Element</div>
<div>Element</div>
<div>Element</div>
<div>Element</div>
</div>
<div class="hidden-xs col-md-3">
<ul class="list-group">
<li class="list-group-item">
<span class="badge">12</span>
Matt Damon
</li>
<li class="list-group-item">
<span class="badge">9</span>
Brad Pitt
</li>
<li class="list-group-item">
<span class="badge">4</span>
George Clooney
</li>
<li class="list-group-item">
<span class="badge">2</span>
Angelina Jolie
</li>
</ul>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
考虑以下 AWS Lambda 函数:
var i = 0;
exports.handler = function (event, context) {
context.succeed(++i);
};
Run Code Online (Sandbox Code Playgroud)
多次执行此函数,我最终得到类似如下的输出:
> 0
> 1
> 2
> 0
> 1
> 0
> 3
> 2
> 4
> 1
> 2
Run Code Online (Sandbox Code Playgroud)
如您所见,脚本似乎有 3 个单例,当我执行该函数时,我随机地以其中一个结束。
这是预期的行为吗?我在文档中找不到任何相关信息。
我问这个是因为我打算连接到 MySQL 并保留一个连接池:
var MySQL = require('mysql');
var connectionPool = MySQL.createPool({
connectionLimit: 10,
host: '*****',
user: '*****',
pass: '*****',
database: '*****'
});
function logError (err, callback) {
console.error(err);
callback('Unable to perform operation');
}
exports.handler = function (event, …
Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×5
dictionary ×3
linq ×3
aws-lambda ×1
c#-4.0 ×1
caching ×1
css ×1
dynamic ×1
generics ×1
html ×1
ienumerable ×1
inheritance ×1
interface ×1
lambda ×1
memory ×1
oop ×1