我已经开始使用一些.NET 3.5代码,并发现以下扩展方法用于缓存::
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key,Func<TKey,TValue> factory,bool useLocking)
{
TValue value;
if(!@this.TryGetValue(key,out value))
{
if (useLocking)
{
lock ((@this as ICollection).SyncRoot)
{
if (!@this.TryGetValue(key, out value))
{
@this[key] = value = factory(key);
}
}
}
else
{
@this[key] = value = factory(key);
}
}
return value;
}
Run Code Online (Sandbox Code Playgroud)
有问题的缓存由字符串键和useLocking = true键控.它总是被这种方法访问(没有流浪TryGetValue).使用该SyncRoot属性也没有问题,因为字典是私有的,没有其他地方使用它.双重锁定是危险的,因为字典在写入时不支持读取.虽然技术上没有报告任何问题,因为产品没有发货,我觉得这种方法会导致竞争条件.
切换Dictionary<,>到a Hashtable.我们将失去类型安全性,但我们将能够支持我们追求的并发模型,(1位作者,多位读者).
删除外部TryGetValue.这样每次读取都需要锁定.这可能对性能有害,但获得无争议的锁应该相当便宜.
两者都很糟糕.有人有更好的建议吗?如果这是.NET 4代码,我只需将其切换为a ConcurrentDictionary,但我没有这个选项.
我知道如果我们使用datacontract,我们可以对类进行更多控制,但是,请考虑以下2种情况
[DataContract]
public class Customer
{
[DataMember]
public string CustomerName {get; set;}
[DataMember]
public int Age{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Customer
{
public string CustomerName {get; set;}
public int Age{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
它们都在.net客户端上正确序列化.我个人不用第二个例子.任何人都可以指出我在两个班级的差异吗?我打算发送这两个类中的所有公共属性.
将文件(例如AVI)保存为数据存储区中的Blob与将其保存为网络服务器上的普通AVI文件有什么区别?
谢谢!
乔尔
我有一个Dictionary对象需要写入XML文件.字典包含String类型作为Key,自定义类的Object(从System.Windows.Forms.Control派生)作为Value.
namespace SharpFormEditorDemo
{
[Serializable]
public static class common
{
public static Dictionary<String,CommonControl > dicControls = new Dictionary<string, CommonControl>();
public static Object objSelected = new Object();
public static int ctrlId = 0;
//The serialization and Deserialization methods.
public static void Serialize(XmlTextWriter xmlTextWriter,Dictionary<String,CommonControl> dic)
{
xmlTextWriter.WriteStartDocument();
ControlSerializer file = new ControlSerializer(dic);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ControlSerializer));
xmlSerializer.Serialize(xmlTextWriter, file);
xmlTextWriter.WriteEndDocument();
}
}
Run Code Online (Sandbox Code Playgroud)
CommonControl类是这样的
namespace SharpFormEditorDemo
{
public class CommonControl : System.Windows.Forms.Control
{
//private List<String> controls;
private String sql;
private int minVal; //Minimum …Run Code Online (Sandbox Code Playgroud) 就是想...
有没有理由不使用受保护的属性?
我的意思是代替使用它:
public abstract class Foo
{
protected Bar { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
使用这个:
public abstract class Foo
{
private Bar _bar;
protected Foo(Bar bar)
{
_bar = bar;
}
protected GetBar()
{
return _bar;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有值的C#列表框
Profile 1
Profile 2
Profile 3
Run Code Online (Sandbox Code Playgroud)
我想在表单加载时选择配置文件2.我该怎么做呢?
部署了wcf服务(.net 4.0).服务端配置如下所示:
<endpoint address=""
binding="webHttpBinding"
bindingNamespace="https://mydomain/myservice/services"
behaviorConfiguration="WebBehavior"
contract="MyService" />
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)
在Web应用程序中尝试使用服务,web.config如下所示:
<system.serviceModel>
<client>
<endpoint name="MyServiceEndpointBasicHttp"
address="http://myDomain/myService"
binding="webHttpBinding" behaviorConfiguration="webBehavior"
contract="MyNamespace.IMyService" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
我在拨打服务时遇到异常:
合同'IMyService'的操作'Method1'指定要序列化的多个请求体参数,而不包含任何包装元素.最多可以在没有包装元素的情况下序列化一个body参数.删除额外的body参数或将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为Wrapped.
经过一些谷歌搜索,我们已经开始[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Xml)]采用方法,但没有成功......
一件有趣的事情:异常中始终存在相同的方法名称,即使我正在调用其他方法......
通过输入方法名称和必要的参数,使用浏览器进行测试时,服务在REST模式下正常工作......
我正在尝试使用 C# 中的实体框架从表中获取一行。我有一个名为“TipoPlanta”的表,带有一个名为“Tipo”的主键,它的类型是字符串。
当我尝试使用字符串从表中获取一行时,如果我使用字符串文字,我只能找到一些东西。如果我使用传递给方法的字符串,我找不到任何行。
我有以下方法,其中添加了一些我一直在尝试调试的内容。我传递了字符串 tipoString,在本例中它的值为“Arbol persistente”。这是代码:
private TipoPlanta getTipoPlanta(String tipoString)
{
try
{
if (tipoString == "Arbol persistente")
Console.WriteLine("They are the same");
else
Console.WriteLine("They are different");
var result = (from tar in plantaContext.TipoPlanta where tar.Tipo.Contains(tipoString) select tar);
var sql = ((System.Data.Objects.ObjectQuery)result).ToTraceString();
Console.WriteLine("SQL = " + sql);
Console.WriteLine("RESULT COUNT = " + result.Count());
Console.WriteLine();
var resultLiteral = (from tar in plantaContext.TipoPlanta where tar.Tipo.Contains("Arbol persistente") select tar);
var sql2 = ((System.Data.Objects.ObjectQuery)resultLiteral).ToTraceString();
Console.WriteLine("SQL2 = " + sql2);
Console.WriteLine("RESULT LITERAL COUNT = " …Run Code Online (Sandbox Code Playgroud) 我在这里编写了一个用于插入和删除操作的WCF服务,我们使用泛型方法,但是它给出了以下错误"System.Runtime.Serialization.InvalidDataContractException:类型'T'不能作为模式类型导出,因为它是一个开放的泛型类型.你可以如果所有通用参数类型都是实际类型,则只导出泛型类型."
这里"EntityBase2"是所有实体的基类
[ServiceContract]
[ServiceKnownType(typeof(EntityBase2))]
public interface IBackupUtility
{
[OperationContract]
void Delete<T>(T entity) where T : EntityBase2;
[OperationContract]
void InsertORUpdate<T>(T entity) where T : EntityBase2;
}
Run Code Online (Sandbox Code Playgroud)
问题是我如何暴露泛型类型'T'?
有没有办法在一个TextBlock中组合静态文本和绑定?因为StringFormat在Windows Phone 7中不起作用.我试试
<TextBlock Text="{Binding strAudioArtistName, StringFormat=StaticText: {0}}"/>
Run Code Online (Sandbox Code Playgroud)
但不要工作....
谢谢
c# ×5
.net ×3
wcf ×3
concurrency ×1
database ×1
datacontract ×1
filesystems ×1
linq ×1
listbox ×1
listboxitem ×1
protected ×1
rest ×1
selected ×1
silverlight ×1
winforms ×1
xml ×1