为什么以下代码不起作用:
class Program
{
static void Main ( string[ ] args )
{
SomeClass s = new SomeClass( );
s.GetType( ).GetField( "id" , System.Reflection.BindingFlags.NonPublic ) // sorry reasently updated to GetField from GetProperty...
.SetValue( s , "new value" );
}
}
class SomeClass
{
object id;
public object Id
{
get
{
return id;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图设置私有字段的值.
这是我得到的例外:
System.NullReferenceException未处理Message = Object引用未设置为对象的实例.来源= ConsoleApplication7
StackTrace:位于C:\ Users\Antonio\Desktop\ConsoleApplication7\ConsoleApplication7\Program.cs中的Program.Main(String [] args):位于System的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)的第18行. System.Threading.ExecutionContext.Run上的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()中的AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)(ExecutionContext executionContext System.Threading.ThreadHelper.ThreadStart()中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态),ContextExallback回调,对象状态,布尔值ignoreSyncCtx:InnerException:
我们都知道该using语句非常适合您想要及时清理的资源,例如打开文件或数据库连接.
我想知道在资源清理不是Dispose()方法的目标而是重置为先前状态的情况下使用该语句是否是一件好事.
例如,一个允许using语句包装过程的类,该过程需要花费大量时间并将Cursor更改为等待状态.
class CursorHelper : IDisposable
{
readonly Cursor _previousState;
public CursorHelper(Cursor newState)
{
_previousState = Cursor.Current;
Cursor.Current = newState;
}
public void Dispose()
{
Cursor.Current = _previousState;
}
}
Run Code Online (Sandbox Code Playgroud)
然后可以这样使用类,而不必担心在完成后还原Cursor.
public void TimeIntensiveMethod()
{
using (CursorHelper ch = new CursorHelper(Cursors.WaitCursor))
{
// something that takes a long time to complete
}
}
Run Code Online (Sandbox Code Playgroud)
这是否适用于该using声明?
所以我试图改变图像并将它们变成一个循环,我想出了这个脚本,我遇到的问题是,当它改变到第二张图片时,javascript 会冻结在我身上,我知道它while(true) 部分,但我不知道如何修复它,请帮忙。
感谢您
var images = new Array();
images[0] = "image1.jpg";
images[1] = "image2.jpg";
images[2] = "image3.jpg";
setTimeout("changeImage()", 3000);
var x=0;
function changeImage()
{
while(true){
document.getElementById("img").src=images[x]
x++;
}
if (x=2){
x=0;
}
}
Run Code Online (Sandbox Code Playgroud)