可能重复:
弱引用 - 它们有用吗?
由于垃圾收集器可以随时声明弱引用,是否有任何实际的理由使用它们?
我最近遇到了一段带有WeakReferences的Java代码 - 我从未见过它们已部署,尽管我们在介绍时遇到过它们.这是应该常规使用的东西还是只有在遇到内存问题时?如果是后者,它们是否可以轻松改装或代码是否需要严格的重构?普通的Java(或C#)程序员通常会忽略它们吗?
编辑过度使用WR可以造成任何损害吗?
在C#中是否可以扩展一个类,而不仅仅是添加函数而是属性.例如:我有一个我依赖的标准DLL库,供应商不想修改它.
在整个代码中我已经广泛使用了DataCell类,现在才意识到我需要为它添加一个额外的属性,因为创建一个继承自这个类的新扩展类看起来不会起作用+大量的重写.
DataCell [元数据]
public class DataCell : Message
{
public int Field1;
public int Field2;
public DataCell()
{
..
}
..
}
Run Code Online (Sandbox Code Playgroud)
基本上我想添加一个public int Flags; 到这堂课.所以我现在可以做而不重写任何东西,(新的DataCell).Flags = 0x10;
我正在尝试将此类移植到EF核心:
但是我有这个问题:
错误CS0246:找不到类型或命名空间名称'IObjectContextAdapter'(您是否缺少using指令或程序集引用?)(CS0246)(Mehdime.Entity)
还有这个:
错误CS0246:找不到类型或命名空间名称'ObjectStateEntry'(您是否缺少using指令或程序集引用?)(CS0246)(Mehdime.Entity)
所有nuget包已经在.
/*
* Copyright (C) 2014 Mehdi El Gueddari
* http://mehdi.me
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Mehdime.Entity
{
public class DbContextScope : IDbContextScope
{
private bool _disposed;
private bool _readOnly;
private bool _completed;
private bool _nested;
private DbContextScope …Run Code Online (Sandbox Code Playgroud) c# entity-framework entity-framework-core .net-core asp.net-core
在.net中是否有任何方法可以在运行时将属性字典绑定到实例,即,基本对象类具有如下属性:
public IDictionary Items { get; }
Run Code Online (Sandbox Code Playgroud)
我想出了一个涉及静态字典和扩展方法的解决方案
void Main()
{
var x = new object();
x.Props().y = "hello";
}
static class ExpandoExtension {
static IDictionary<object, dynamic> props = new Dictionary<object, dynamic>();
public static dynamic Props(this object key)
{
dynamic o;
if (!props.TryGetValue(key, out o)){
o = new ExpandoObject();
props[key] = o;
}
return o;
}
}
Run Code Online (Sandbox Code Playgroud)
但这会阻止对象获取GC,因为props集合包含引用.事实上,这对于我的特定用例来说还不错,因为一旦我完成了我正在使用它们的特定事物,我可以手动清除道具,但我想知道,是否有一些狡猾的方式来绑定ExpandoObject到键,同时允许垃圾回收?