偶尔我喜欢花一些时间查看.NET代码,看看事情是如何在幕后实现的.我在String.Equals通过Reflector 查看方法时偶然发现了这个宝石.
C#
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public override bool Equals(object obj)
{
string strB = obj as string;
if ((strB == null) && (this != null))
{
return false;
}
return EqualsHelper(this, strB);
}
Run Code Online (Sandbox Code Playgroud)
IL
.method public hidebysig virtual instance bool Equals(object obj) cil managed
{
.custom instance void System.Runtime.ConstrainedExecution.ReliabilityContractAttribute::.ctor(valuetype System.Runtime.ConstrainedExecution.Consistency, valuetype System.Runtime.ConstrainedExecution.Cer) = { int32(3) int32(1) }
.maxstack 2
.locals init (
[0] string str)
L_0000: ldarg.1
L_0001: isinst string
L_0006: stloc.0
L_0007: ldloc.0
L_0008: brtrue.s L_000f
L_000a: …Run Code Online (Sandbox Code Playgroud) Console.WriteLine在输出写入或立即返回之前是否阻塞?
如果它确实阻塞了是否有一种向控制台写入异步输出的方法?
显然我做错了什么,但是在使用DataType属性时,我似乎无法使HierarchicalDataTemplate(甚至只是DataTemplate)工作.我已经创建了最短的WPF应用程序来演示这个问题.
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<HierarchicalDataTemplate DataType="x:Type local:Foo">
<TextBlock Text="I am a Foo" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="x:Type System:String">
<TextBlock Text="I am a String" />
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Name="treeView1" ItemsSource="{Binding}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
码:
namespace WpfApplication1
{
public class Foo
{
public string Name { get; set; }
}
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var list = new …Run Code Online (Sandbox Code Playgroud) 我有一个名单Population,是很多位置的很好的列表,在某些时候我停止使用它.我如何释放资源?那么这是代码的一部分:
private List <BasePopulation> Population=new List <BasePopulation>();
Population.SomeMethod();
Population.Clear();
Run Code Online (Sandbox Code Playgroud)
我使用了Clear方法,但没有用.任何的想法?
当无效输入传递给方法或对象即将进入无效状态时,我们通常抛出异常.让我们考虑以下示例
private void SomeMethod(string value)
{
if(value == null)
throw new ArgumentNullException("value");
//Method logic goes here
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我插入了抛出的throw语句ArgumentNullException.我的问题是运行时如何设法抛出ThreadAbortException.显然,throw在所有方法中都不可能使用语句,即使运行时也设法引入ThreadAbortException我们的自定义方法.
我想知道他们是怎么做到的?我很想知道幕后发生了什么,我打开了一个反射器打开Thread.Abort并结束了这个
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void AbortInternal();//Implemented in CLR
Run Code Online (Sandbox Code Playgroud)
然后我用Google搜索并发现这个ThreadAbortException如何真正起作用.这个链接说运行时通过QueueUserAPC函数发布APC ,这就是他们如何做到这一点.我不知道QueueUserAPC方法我只是试着看一下代码是否可行.以下代码显示了我的尝试.
[DllImport("kernel32.dll")]
static extern uint QueueUserAPC(ApcDelegate pfnAPC, IntPtr hThread, UIntPtr dwData);
delegate void ApcDelegate(UIntPtr dwParam);
Thread t = new Thread(Threadproc);
t.Start();
//wait for thread to start
uint result = QueueUserAPC(APC, new IntPtr(nativeId), (UIntPtr)0);//returns zero(fails)
int error = …Run Code Online (Sandbox Code Playgroud) 在您的实际编程经验中,STACK和HEAP的这些知识如何在现实生活中拯救您?来自战壕的任何故事?或者这个概念是否适合填写编程书籍并有利于理论?
如果我的用户在加利福尼亚,并且他们的计算机设置为PST,则那是下午1:00.如果我的服务器设置为EST,则当前服务器时间是下午4:00.
我需要一种方法来获得客户端和服务器之间的时区差异,无论是在Javascript还是C#中.在我的例子中,我会得到3(或-3,无所谓).
有谁知道如何做到这一点?
编辑:RedFilter的可能解决方案
在javascript中完成所有操作:
serverDate = new Date('<%= DateTime.Now.ToString() %>');
clientDate = new Date();
diffMin = (serverDate.getTime()-clientDate.getTime())*1000*60; //get difference in minutes
Run Code Online (Sandbox Code Playgroud)
认为这会奏效吗?或者两者都会同时返回?
我只是想确保我在这里理解一些东西.如果我获得锁定Monitor.Enter或者lock我需要Pulse在释放锁之前打电话?
Monitor.Enter
Monitor.Pulse // Is that neccessary?
Monitor.Exit
Run Code Online (Sandbox Code Playgroud)
总是让我相信一个Monitor.Exit电话会隐含地调用Monitor.Pulse.这是真的吗?
我不确定你什么时候会用Pulse.这就是造成混乱的原因.
我最近在一个类似于下面的测试中遇到了一个面试问题,我没有很多使用线程开发的经验,有人可以帮助建议我如何处理这个问题吗?:
public class StringQueue
{
private object _lockObject = new object();
private List<string> _items = new List<string>();
public bool IsEmpty()
{
lock (_lockObject)
return _items.Count == 0;
}
public void Enqueue(string item)
{
lock (_lockObject)
_items.Add(item);
}
public string Dequeue()
{
lock (_lockObject)
{
string result = _items[0];
_items.RemoveAt(0);
return result;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上方法的线程是否安全?为什么?
public string DequeueOrNull()
{
if (IsEmpty())
return null;
return Dequeue();
}
Run Code Online (Sandbox Code Playgroud) 我这样做时会一直遇到以下异常:
Using cnn As SqlConnection = New SqlConnection(ConnectionStr)
cnn.Open() 'I am fine up to here'
End Using 'Here I am getting the following exception'
Run Code Online (Sandbox Code Playgroud)
手动调用cnn.Dispose()会导致相同的异常.在我的代码中的大多数地方似乎都可以,但只是在这个函数中,我无法关闭我打开的连接,因为我不断收到ThreadAbortException.我很难过,有什么想法吗?任何提示?这是我得到的例外:
System.TypeInitializationException: The type initializer for 'System.Data.ProviderBase.DbConnectionClosedPreviouslyOpened' threw an exception. ---> System.Threading.ThreadAbortException: Exception of type 'System.Threading.ThreadAbortException' was thrown.
--- End of inner exception stack trace ---
at System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlInternalConnection.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Close()
at System.Data.SqlClient.SqlConnection.Dispose(Boolean disposing)
Run Code Online (Sandbox Code Playgroud)