½âÎöC#ÖÐisºÍas²Ù×÷·ûµÄÓ÷¨
c# ÖÐ isºÍas ²Ù×÷·ûÊÇÓÃÀ´½øÐÐÇ¿ÖÆÀàÐÍת»»µÄ
is : ¼ì²éÒ»¸ö¶ÔÏóÊÇ·ñ¼æÈÝÓÚÆäËûÖ¸¶¨µÄÀàÐÍ,²¢·µ»ØÒ»¸öBoolÖµ,ÓÀÔ¶²»»áÅ׳öÒì³£
object o = new object();
if (o is Label)
{
Label lb = (Label)o;
Response.Write("ÀàÐÍת»»³É¹¦");
}
else
{
Response.Write("ÀàÐÍת»»Ê§°Ü");
}
ÔÚÉÏÃæµÄ´úÂë,CLRʵ¼ÊÉÏ»á¼ì²éÁ½´Î¶ÔÏóµÄÀàÐÍ,is²Ù×÷·ûÏȺËʵһ´Î,Èç¹ûo¼æÈÝÓÚLable,ÄÇôÔÚ(Label)oʱ»áÔٴκËʵһ´Î,ЧÂʱȽϵÍ,²»½¨ÒéʹÓÃ
as:ÓëÇ¿ÖÆÀàÐÍת»»ÊÇÒ»ÑùµÄ,µ«ÊÇÓÀÔ¶²»»áÅ׳öÒì³£,¼´Èç¹ûת»»²»³É¹¦,»á·µ»Ønull
object o = new object();
Label lb = o as Label;
if (lb == null)
{
Response.Write("ÀàÐÍת»»Ê§°Ü");
}
else
{
Response.Write("ÀàÐÍת»»³É¹¦");
}
ÔÚÉÏÃæµÄ´úÂëÖÐ,CLRÖ»»á½øÐÐÒ»´ÎÀàÐͺËʵ,ЧÂÊÒª¸ßÓÚ is
ÔÚѧϰC#ʱ£¬¿´µ½ÕâÑùÒ»ÖÖ±í´ï·½Ê½£º
BAP_USER user = session["User"] as BAP_USER;
if(user == null)
return mapping.FindForward("error");
ÓÐЩ²»Ã÷°×£¬ÉÏÍø²éÁËЩ×ÊÁÏ£¬¶ÔisºÍas²Ù×÷·ûÓÐÁËЩÁ˽⣬°Ñ×ÊÁÏ·ÅÔÚÕâÀ﹩ѧϰºÍ²Î¿¼£º
ÔÚC#ÖУ¬ËùÓеĶ«Î÷¶¼ÊǶÔÏó¡£Òò´ËÈκγ£ÊýÒ²ÊÇÒ»¸öÕûÐͶÔÏó¡£ÕâÀïÓõ½ÁËas £¬as ÊÇC#ÓïÑÔÀïÃæµÄÒ»¸ö¹Ø¼ü×Ö¡£as ÔËËã·ûÀàËÆÓÚÀàÐÍת»»£¬Ëù²»Í¬µÄÊÇ£¬µ±×ª»»Ê§°Üʱ£¬as ÔËËã·û½«²úÉú¿Õ£¬¶ø²»ÊÇÒý·¢Òì³£¡£ÔÚÐÎʽÉÏ£¬ÕâÖÖÐÎʽµÄ±í´ïʽ£º
expression as type
as ÔËËã·ûÖ»Ö´ÐÐÒýÓÃת»»ºÍ×°Ïäת»»¡£
as ÔËËã·ûÎÞ·¨Ö´ÐÐÆäËûת»»£¬ÈçÓû§¶¨ÒåµÄת»»£¬ÕâÀàת»»Ó¦Ê¹ÓÃcase±í´ïʽÀ´´úÌæÆäÖ´ÐÐ
¹ØÓÚisµÄÆäʵҲ²î²»¶à£¬Ö»ÊÇIS²»»á°ïÄã½øÐÐÈκεÄת»»¡£ËùÒÔÐèÒª×ÔÒѽøÐÐÏÔʾת»»
is²Ù×÷·û½øÐÐת»»£¬Ëü×Ô¶¯¼ì²éʱ¾ÖÊÇ·ñºÍÀàÐͼæÈÝ£¬²¢·µ»Ø½á¹û¡£¶øÇÒËü²»»áÅ׳öÒì³£¡£Èç¹û¶ÔÏóÒýÓÃΪnull£¬Ôòis×Ü·µ»Øfalse¡£
if (cls1 is Class2)
{
Class2 cls2 = (Class2)cls1;
}
else
System.Console.WriteLine("Error 2!");
as²Ù×÷·û½øÐÐת»»£º
Class2 cls2 = cls1 as Class2;
if (cls2!=null)
System.Console.WriteLine("Ok");
else
System.Console.WriteLine("Error!");
¶Ô±ÈisºÍas²Ù×÷·ûת»»µÄÁ½ÖÖ·½Ê½£¬isÐèÒª×öÁ½´Î¶ÔÏóµÄÀàÐͼì²é£¬¶øasÐèÒª×öÒ»´Î¶ÔÏóÀàÐͼì²é£¬ÔÙ¼ÓÒ»´ÎnullµÄ¼ì²é£¬¶ønull¼ì²é¿ªÏú±È¶ÔÏóÀà
Ïà¹ØÎĵµ£º
DBHelper:
/// <summary>
/// Ö´Ðвéѯ
/// </summary>
/// <param name="sql">ÓÐЧµÄselectÓï¾ä</param ......
1 ¸¸Àà partial class FormBillTemplet:Form
/// <summary>
/// е¥
/// </summary>
/// <param name="s ......
ÔÚC#.netÖÐÈçºÎ²Ù×÷XML
¿ÉÄܺܶàÅóÓÑ»¹Ã»ÓнøÐйýXML²Ù×÷,Ï£ÍûÏÂÃæÕâƪÎÄÕÂÄܶԴó¼ÒÓаïÖú.»òÕß×Ô¼º¸´ÖÆһϴúÂëÊÔÒ»ÏÂ.ÕâƪÎÄÕ²»ÊÇÎÒдµÄ.ÎÒÖ»ÊÇת¹ýÀ´.¾õµÃ²»´í.
ÐèÒªÌí¼ÓµÄÃüÃû¿Õ¼ä£º
using System.Xml;
¶¨Ò弸¸ö¹«¹²¶ÔÏó£º
XmlDocument xmldoc ;
XmlNode xmlnode ;
XmlElement xmlelem ;
1£¬´´½¨µ½·þÎñÆ÷ͬà ......
¸Õ¿ª²©²»¾Ã,¾ÍºÜÏëдµã¶«Î÷,µ«ÊdzöÓÚÖÖÖÖÔÒò¶¼Ã»¶¯ÊÖ¡£ÓÐÁ˲©¿Í×ܲ»ÄÜ¿Õ×Å£¬¾ÍÔÚ×ÁÄ¥×ÅдЩʲô¡£¸ÕºÃѧϰÁË×Ö·û´®µÄһЩ¶«Î÷£¬×öÁËϱʼǸоõ»¹²»´í£¬ÄóöÀ´É¹É¹...
ÏÈ˵˵×Ö·û´®²Ù×÷µÄÐÔÄÜÎÊÌâ¡£C#ÖеÄ×Ö·û´®²Ù×÷£¬ÓõÃÒ²±È½Ï¶à£¬´ó¼Ò¶¼²»Ä°Éú¡£·´Õý¾ÍÊÇÉùÃ÷Ò»¸ö×Ö·û´®±äÁ¿£¬È»º ......
//»ñÈ¡°üº¬Çåµ¥µÄÒѼÓÔØÎļþµÄ·¾¶»ò UNC λÖá£
public static string sApplicationPath = Assembly.GetExecutingAssembly ( ).Location;
//result: X:\xxx\xxx\xxx.dll (.dllÎļþËùÔÚµÄĿ¼+.dllÎļþÃû)
//»ñÈ¡µ±Ç°½ø³ÌµÄÍêÕû·¾¶£¬°üº¬ÎļþÃû(½ø³ÌÃû)¡£
string str = this.GetType ( ).Assembly.Location;
//result ......