无论我读了多少关于这两种模式,我都看不出差异.
我正在寻找像这样的示例用法:
Foo<string> stringFoo = new Foo<string>("The answer is");
Foo<int> intFoo = new Foo<int>(42);
// The Value of intFoo & stringFoo are strongly typed
stringFoo.Nullify();
intFoo.Nullify();
if (stringFoo == null && intFoo == null)
MessageBox.Show("Both are null);
Run Code Online (Sandbox Code Playgroud)
鉴于此类Foo,我可以将T自动换行为可空:
public class Foo1<T>
where T : struct
{
private T? _value;
public Foo(T? initValue)
{
_value = initValue;
}
public T? Value { get { return _value; } }
public void Nullify { _value = null; }
}
Run Code Online (Sandbox Code Playgroud)
这适用于基元,但不适用于String或其他类.
下一个flavor适用于字符串,但不适用于原语:
public class Foo2<T> …Run Code Online (Sandbox Code Playgroud) 我想创建一个TypeConverter泛型类,如下所示:
[TypeConverter(typeof(WrapperConverter<T>))]
public class Wrapper<T>
{
public T Value
{
// get & set
}
// other methods
}
public class WrapperConverter<T> : TypeConverter<T>
{
// only support To and From strings
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo …Run Code Online (Sandbox Code Playgroud) c# typeconverter visual-studio-2010 asp.net-4.0 asp.net-web-api
这个网站的新用户,如果我没有以可接受的方式发帖,请告诉我.
我经常在下面的示例中编写一些代码(为了清楚起见,使用Dispose ommtited等内容).我的问题是,如图所示需要挥发物吗?或者,当我读过Thread.Start时,ManualResetEvent.Set是否有隐式内存屏障?或者显式的MemoryBarrier调用是否比挥发性更强?还是完全错了?此外,据我所见,某些操作中的"隐式记忆障碍行为"没有记录,这是非常恐怖的,这些操作的列表是否存在?
谢谢,汤姆
:
class OneUseBackgroundOp
{
// background args
private string _x;
private object _y;
private long _z;
// background results
private volatile DateTime _a
private volatile double _b;
private volatile object _c;
// thread control
private Thread _task;
private ManualResetEvent _completedSignal;
private volatile bool _completed;
public bool DoSomething(string x, object y, long z, int initialWaitMs)
{
bool doneWithinWait;
_x = x;
_y = y;
_z = z;
_completedSignal = new ManualResetEvent(false);
_task = new Thread(new ThreadStart(Task));
_task.IsBackground …Run Code Online (Sandbox Code Playgroud) 根据标题,我发现我的只读模型属性没有在我的Web API项目中序列化.MVC 4 Web API,VS2010.
我已经看过很多帖子,比如这个stackoverflow问题,说明MVC 4 Web API beta不支持JSON序列化只读属性.但是许多其他参考文献指出最终版本使用的是JSON.NET而不是DataContractJsonSerializer,所以应该解决这个问题.
这个问题是否得到了解决?如果没有,我是否只是为了获得序列化而被迫投入假冒产品?
校正,它也似乎与JSON工作(对不起!),但XML表现出的问题.与之前相同的问题,但在XML序列化的上下文中.
我遇到了从正常的异步ApiController返回"立即"响应的问题.代码如下,寻找感叹号.用例是内容类型检查失败,我想发回错误响应消息.第一个版本挂起了Visual Studion 2010(和Fiddler).第二部作品.
我的问题是,为什么我不能使用我的初始方法返回仅仅传回响应对象的虚拟任务?
public class MyController : ApiController
{
public Task<HttpResponseMessage> Post([FromUri]string arg)
{
HttpResponseMessage response = null;
// synchronous validation
if (Request.Content.Headers.ContentType.MediaType != @"image/jpeg")
{
response = Request.CreateErrorResponse(
HttpStatusCode.UnsupportedMediaType,
"Invalid Content-Type.");
}
if (response == null) // no immediate response, switch to async
{
// work done here
}
else // immediate response, but we need to wrap in a task for caller to fetch
{
// !!!! this one doesn't work !!!
return new Task<HttpResponseMessage>( () => …Run Code Online (Sandbox Code Playgroud) 这是场景.我有一个将由多个线程(ASP.NET)访问的类,可以将结果存储在一次写入,多次读取的缓存中.此高速缓存的对象是无法作为静态初始化程序的一部分执行的操作的结果,但必须等待第一次执行.所以我实现了一个简单的null检查,如下所示.我知道如果两个线程在同一时刻点击此检查,我将计算两次ExpensiveCalculation,但这不是世界末日.我的问题是,我是否需要担心由于优化或其他线程缓存,静态_cachedResult仍然被其他线程视为null.一旦编写,该对象只被读取,所以我不认为需要全面锁定.
public class Bippi
{
private static ExpensiveCalculation _cachedResult;
public int DoSomething(Something arg)
{
// calculate only once. recalculating is not harmful, just wastes time.
if (_cachedResult == null);
_cachedResult = new ExpensiveCalculation(arg);
// additional work with both arg and the results of the precalculated
// values of _cachedResult.A, _cachedResult.B, and _cachedResult.C
int someResult = _cachedResult.A + _cachedResult.B + _cachedResult.C + arg.ChangableProp;
return someResult;
}
}
public class ExpensiveCalculation
{
public int A { get; private set; }
public int …Run Code Online (Sandbox Code Playgroud) VB noob在这里,处理遗留的VB 6.0应用程序.
当我在下面的函数中检查lineno的值时,我得到了预期的值:
Public Function GetNumOfLines(filename As String) As Integer
Dim lineno as Integer
lineno = 0
Open App.Path + filename For Input As #1
Do While Not EOF(1)
lineno = lineno + 1
Line Input #1, linevar
Loop
Close #1
MsgBox "numOfLines: " & lineno 'This works
End Function
Run Code Online (Sandbox Code Playgroud)
但是当我从GetATRNames(下面)调用GetNumOfLines时,numOfLines为0:
Public Function GetATRNames() As String()
Dim filename as String
filename = "\atrname.dat"
Dim numOfLines as Integer
numOfLines = GetNumOfLines(filename)
MsgBox "numOfLines: " & numOfLines 'This does not
End …Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×3
volatile ×2
asp.net-4.0 ×1
c#-4.0 ×1
generics ×1
integration ×1
static ×1
vb.net ×1
vb6 ×1