草庐IT

CACHE_VAR_TYPE

全部标签

c# - 在C#中将 'var'的值初始化为null

这个问题在这里已经有了答案:Howtoinitializevar?(11个答案)关闭8年前。我想给一个变量赋初值null,并在下一个if-elseblock中赋值,但是编译器报错,Implicitly-typedlocalvariablesmustbeinitialized.我怎样才能做到这一点?

C# HttpRuntime.Cache.Insert() 不持有缓存值

我正在尝试使用HttpRuntime.Cache.Insert()来缓存价格值,但在清除之前似乎只保留了几个小时或更长时间。我究竟做错了什么?我希望该值在缓存中保留3天。HttpRuntime.Cache.Insert(CacheName,Price,null,DateTime.Now.AddDays(3),TimeSpan.Zero); 最佳答案 简答您的应用程序池或网站关闭得太早了。延长站点的空闲超时,延长运行该站点的池的应用程序池生命周期。提高内存分配和请求限制。完整答案如果您想知道什么时候以及为什么从缓存中删除某些内容,您需

C# 泛型 : cast generic type to value type

我有一个泛型类,它保存指定类型T的值。该值可以是int、uint、double或float。现在我想获取值的字节以将其编码为特定协议(protocol)。因此我想使用方法BitConverter.GetBytes()但不幸的是Bitconverter不支持泛型类型或undefinedobject。这就是为什么我要转换值并调用GetBytes()的特定重载。我的问题:如何将通用值转换为int、double或float?这不起作用:publicclassGenericClasswhereT:struct{T_value;publicvoidSetValue(Tvalue){this._va

c# - var 与具体类型用法

这个问题在这里已经有了答案:UseofvarkeywordinC#(86个答案)Differencebetween"var"and"object"inC#[duplicate](6个答案)WhydoesReSharperwanttouse'var'foreverything?[duplicate](23个回答)关闭9年前。我已经在stackoverflow中检查了5个或更多关于var使用的帖子,但我仍在寻找关于var使用的答案。我习惯使用具体类型而不是var,但我的Resharper提示要更改为var。var是类型的选择-即使具体类型已知?

c# - 为什么不能将 Type 用作常量值?

引用MSDN-const(C#reference):Aconstantexpressionisanexpressionthatcanbefullyevaluatedatcompiletime.Therefore,theonlypossiblevaluesforconstantsofreferencetypesarestringandanullreference.根据:typeof(T)vs.Object.GetType()performance,typeof(T)是一个编译时表达式。那么为什么Type不能是常量值呢?以下代码不会编译:publicconstTypeINT_TYPE=ty

c# - .NETStandard 1.0/.NET Core 中 Type.GetGenericArguments() 的等价物是什么?

.NETStandard1.0中“缺少”方法System.Type.GetGenericArguments(),我认为TypeInfo.GenericTypeArguments是的替代品GetGenericArguments(),但不幸的是,当提供开放泛型类型时,它们的行为会有所不同。以下面的代码为例:Typetype=typeof(ICommandHandler);type.GetGenericArguments();//return{TCommand}type.GetTypeInfo().GenericTypeArguments;//returnsemptyarray虽然GetGe

c# - Dapper 抛出 "Invalid type owner for DynamicMethod."

所以我正在尝试使用Dapper.net,而且我很喜欢它。我不喜欢的是当我尝试批量插入实体时抛出以下错误:DynamicMethod的类型所有者无效。atSystem.Reflection.Emit.DynamicMethod.Init(Stringname,MethodAttributesattributes,CallingConventionscallingConvention,TypereturnType,Type[]signature,Typeowner,Modulem,BooleanskipVisibility,BooleantransparentMethod,StackCra

c# - 如何从基类中获取子类的 `Type`

我有一个抽象基类,我想在其中实现一个方法来检索继承类的特性属性。像这样的……publicabstractclassMongoEntityBase:IMongoEntity{publicvirtualobjectGetAttributeValue(stringpropertyName)whereT:Attribute{varattribute=(T)typeof(this).GetCustomAttribute(typeof(T));returnattribute!=null?attribute.GetType().GetProperty(propertyName).GetValue(a

c# - XML 序列化错误 : 2 types both use the XML type name, 'Relationship',来自命名空间 ''

我在通过XML进行序列化时遇到问题,因为2个类使用一个类(尽管类不同!)称为关系。我尝试使用XML属性用另一个名称装饰其中的一个类,但它仍然给我以下错误:{"Types'SiteServer.Relationship'and'LocalServer.Relationship'bothusetheXMLtypename,'Relationship',fromnamespace''.UseXMLattributestospecifyauniqueXMLnameand/ornamespaceforthetype."}这是我的2个类(class),有人知道为什么吗??我使用了错误的属性吗?它似

c# - 保留一个 Dictionary<Type, MyClass<T>> ,其中元素可以按类型引用

我有一个名为EntityTypeTransform的抽象类,它有一个抽象方法,用于保存将IDataRecord转换为T实例的Func委托(delegate)。publicabstractclassEntityTypeTransformwhereTEntityType:class{publicabstractFuncGetDataTransform();}该类的实现可能看起来像(看起来像):publicclassTaskParameterEntityTypeTransform:EntityTypeTransform{publicoverrideFuncGetDataTransform()