锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 姣忎釜鏍煎紡欏歸兘閲囩敤涓嬮潰鐨勫艦寮忓茍鍖呭惈浠ヤ笅緇勪歡錛?/span> {绱㈠紩[,瀵歸綈][:鏍煎紡瀛楃涓?/span>]} 蹇呴』浣跨敤鎴愬鐨勫ぇ鎷彿錛?/span>“{”鍜?/span>“}”錛夈?/span> 杞箟澶ф嫭鍙?/span> 宸﹀ぇ鎷彿鍜屽彸澶ф嫭鍙瘋瑙i噴涓烘牸寮忛」鐨勫紑濮嬪拰緇撴潫銆傚洜姝わ紝蹇呴』浣跨敤杞箟搴忓垪鏄劇ず鏂囨湰宸﹀ぇ鎷彿鎴栧彸澶ф嫭鍙楓傚湪鍥哄畾鏂囨湰涓寚瀹氫袱涓乏澶ф嫭鍙?/span> ("{{") 浠ユ樉紺轟竴涓乏澶ф嫭鍙?/span> ("{")錛屾垨鎸囧畾涓や釜鍙沖ぇ鎷彿 ("}}") 浠ユ樉紺轟竴涓彸澶ф嫭鍙?/span> ("}")銆傛寜鐓у湪鏍煎紡欏逛腑閬囧埌澶ф嫭鍙風殑欏哄簭渚濇瑙i噴瀹冧滑銆備笉鏀寔瑙i噴宓屽鐨勫ぇ鎷彿銆?/span> 涓嬭〃鎻忚堪浜嗘瘡涓爣鍑嗘牸寮忚鏄庣鐨勬爣鍑嗘牸寮忔ā寮忥紝浠ュ強鍙氳繃璁劇疆鏉ヤ慨鏀規(guī)爣鍑嗘牸寮忕殑鍏寵仈 NumberFormatInfo 灞炴с?/span> ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/fxref_mscorlib/html/b42c00a5-84e5-51a1-b7e4-6aaae8dac25b.htm ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_fxfund/html/580e57eb-ac47-4ffd-bccd-3a1637c2f467.htm 鏍煎紡妯″紡 璇存槑鍜屽叧鑱斿睘鎬?/span> c銆?/span>C 璐у竵鏍煎紡 d銆?/span>D 鍗佽繘鍒舵牸寮?/span> e銆?/span>E 縐戝璁℃暟錛堟寚鏁幫級鏍煎紡 f銆?/span>F 鍥哄畾鐐規(guī)牸寮?/span> g銆?/span>G 甯歌鏍煎紡 n銆?/span>N 鏁板瓧鏍煎紡 p銆?/span>P 鐧懼垎姣旀牸寮?/span> r銆?/span>R 寰榪旀牸寮?/span> x銆?/span>X 鍗佸叚榪涘埗鏍煎紡 http://www.cnblogs.com/wayne-ivan/archive/2014/07/14/3842341.html
]]>
鍦ㄥ綰跨▼緙栫▼涓紝鎴戜滑緇忓父瑕佸湪宸ヤ綔綰跨▼涓幓鏇存柊鐣岄潰鏄劇ず錛岃屽湪澶氱嚎紼嬩腑鐩存帴璋冪敤鐣岄潰鎺т歡鐨勬柟娉曟槸閿欒鐨勫仛娉曪紝Invoke 鍜?nbsp;BeginInvoke 灝辨槸涓轟簡瑙e喅榪欎釜闂鑰屽嚭鐜扮殑錛屼嬌浣犲湪澶氱嚎紼嬩腑瀹夊叏鐨勬洿鏂扮晫闈㈡樉紺恒?br />姝g‘鐨勫仛娉曟槸灝嗗伐浣滅嚎紼嬩腑娑夊強鏇存柊鐣岄潰鐨勪唬鐮佸皝瑁呬負涓涓柟娉曪紝閫氳繃 Invoke 鎴栬?nbsp;BeginInvoke 鍘昏皟鐢紝涓よ呯殑鍖哄埆灝辨槸涓涓鑷村伐浣滅嚎紼嬬瓑寰咃紝鑰屽彟澶栦竴涓垯涓嶄細銆?br />鍐嶄婦涓畝鍗曚緥瀛愯鏄庝笅浣跨敤鏂規(guī)硶,姣斿浣犲湪鍚姩涓涓嚎紼?鍦ㄧ嚎紼嬬殑鏂規(guī)硶涓兂鏇存柊紿椾綋涓殑涓涓猅extBox..
using System.Threading;
//鍚姩涓涓嚎紼?nbsp;
Thread thread=new Thread(new ThreadStart(DoWork));
thread.Start();
//綰跨▼鏂規(guī)硶
private void DoWork()
{
this.TextBox1.Text="鎴戞槸涓涓枃鏈";
}
濡傛灉浣犲儚涓婇潰鎿嶄綔,鍦╒S2005鎴?008閲屾槸浼氭湁寮傚父鐨?img src="http://www.shnenglu.com/Images/dot.gif" alt="" />
姝g‘鐨勫仛娉曟槸鐢↖nvoke\BeginInvoke
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
public delegate void MyInvoke(string str1,string str2);
public Form1()
{
InitializeComponent();
}
public void DoWork()
{
MyInvoke mi = new MyInvoke(UpdateForm);
this.BeginInvoke(mi, new Object[] {"鎴戞槸鏂囨湰妗?/span>","haha"});
}
public void UpdateForm(string param1,string parm2)
{
this.textBox1.Text = param1+parm2;
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(DoWork));
thread.Start();
}
}
}
鐩稿叧璧勬枡錛欼nvoke 鍜?nbsp;BeginInvoke 鐨勭湡姝f兜涔?nbsp;銆佸湪澶氱嚎紼嬩腑濡備綍璋冪敤Winform
Control.BeginInvoke 鏂規(guī)硶 Visual Studio 2010Visual Studio 2010
鍦ㄥ垱寤烘帶浠剁殑鍩虹鍙ユ焺鎵鍦ㄧ嚎紼嬩笂寮傛鎵ц濮旀墭銆?br />鍏叡鏂規(guī)硶 BeginInvoke(Delegate) 鍦ㄥ垱寤烘帶浠剁殑鍩虹鍙ユ焺鎵鍦ㄧ嚎紼嬩笂寮傛鎵ц鎸囧畾濮旀墭銆?nbsp;
鍏叡鏂規(guī)硶 BeginInvoke(Delegate,Object[]) 鍦ㄥ垱寤烘帶浠剁殑鍩虹鍙ユ焺鎵鍦ㄧ嚎紼嬩笂錛岀敤鎸囧畾鐨勫弬鏁板紓姝ユ墽琛屾寚瀹氬鎵樸?nbsp;
public Object EndInvoke(IAsyncResult asyncResult)
鍙傛暟錛歛syncResult 綾誨瀷錛歋ystem.IAsyncResult
IAsyncResult錛岃〃紺虹壒瀹氱殑璋冪敤寮傛鎿嶄綔錛屽湪璋冪敤 BeginInvoke 鏃惰繑鍥炪?nbsp;
]]>
]]>
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TK;
namespace ConsoleTester
{
class Program
{
public class SomeRef
{
public Int32 x;
}
public struct SomeVal
{
public Int32 x;
}
static void Main(string[] args)
{
var r1 = new SomeRef(); //鍫嗕笂鍒嗛厤
var v1 = new SomeVal(); //鏍堜笂鍒嗛厤錛屽悓鏃剁‘淇濆肩被鍨嬪瓧孌靛垵濮嬪寲涓?.
r1.x = 5;
v1.x = 6;
SomeRef r2 = r1;
r2.x = 7;
Console.WriteLine("r1.x={0}", r1.x);
Console.WriteLine("r2.x={0}", r2.x);
var v2 = v1;
v2.x = 8;
Console.WriteLine("v1.x={0}", v1.x);
Console.WriteLine("v2.x={0}", v2.x);
Console.Read();
}
}
}
]]>
{
public enum class LogLevel { Kernel, Fatal, Error, Warning, Infomation, Debug};
public ref class LogHelper
{
public:
static bool Open(String^ filename);
static void Close();
static bool Write(LogLevel level,String^ format, array<Object^>^ args);
static bool WriteLine(LogLevel level,String^ format, array<Object^>^ args);
static void SetLevel(LogLevel level);
};
}
static void MarshalString(String^ net, CString& os) {
LPCTSTR chars = (LPCTSTR)(Marshal::StringToHGlobalUni(net)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
#pragma unmanaged
static CStringA CStrW2CStrA(const CStringW &cstrSrcW)
{
int len = WideCharToMultiByte(CP_UTF8, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);
char *str = new char[len];
memset(str, 0, len);
WideCharToMultiByte(CP_UTF8, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);
CStringA cstrDestA = str;
delete[] str;
return cstrDestA;
}
#pragma managed
]]>
Skin Libraries
Skins for DevExpress components are shipped in compiled libraries. To apply a skin, ensure that a corresponding library is added to the References section of your project.
Common Skins
The DevExpress.Utils.v15.2.dll library contains common skins such as the DevExpress Style, Office 2013, VS2010, Office 2010 Blue, etc. This library is automatically added to the References section of your project when any DevExpress control/component is added to the form.
Extra Skins
Certain additional skins are available in the DevExpress.BonusSkins.v15.2.dll library, which must be manually added to the References section if you need to use any of these bonus skins.
Custom Skins
You can customize any common or extra skin by creating a custom skin using the WinForms Skin Editor utility. The WinForms Skin Editor creates a skin library (.dll) containing custom skins. The created library must be manually added to the References section of your project.
Note
To use any extra or custom skin at runtime, you need to add the skin library registration code. See the Bonus and Custom Skin Registration topic to learn more.
[STAThread]
static void Main() {
// Skin registration.
DevExpress.UserSkins.BonusSkins.Register();
Application.Run(new Form1());
}
浣垮姩鎬佺毊鑲ゅ嵆鍒葷敓鏁?br />It is possible to call the EnableFormSkins() method later, after the application startup. In this instance, the DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged method needs to be called after the call to EnableFormSkins(). This forces the form's title bar to be repainted.
DevExpress.Skins.SkinManager.EnableFormSkins();
DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged();
鎴栬?br />DevExpress.Skins.SkinManager.DisableFormSkins();
DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged();
鍔ㄦ佹崲鐨偆
//娣誨姞鎺т歡 comboBoxEdit1
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
foreach (SkinContainer cnt in SkinManager.Default.Skins)
comboBoxEdit1.Properties.Items.Add(cnt.SkinName);
comboBoxEdit1.SelectedIndexChanged += new EventHandler(comboBoxEdit1_SelectedIndexChanged);
}
void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBoxEdit comboBox = sender as ComboBoxEdit;
string skinName = comboBox.Text;
//DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = skinName;
this.LookAndFeel.SkinName = skinName;
}
}
//using DevExpress.XtraBars.Helpers;
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
this.barManager1.ForceInitialize();
alertControl1.BeforeFormShow += alertControl1_BeforeFormShow;
//紱佹barManage鍙抽敭鏄劇ず
this.barManager1.AllowCustomization = false;
this.barManager1.AllowShowToolbarsPopup = false;
//鍘婚櫎barManage鍙寵竟灝忎笁瑙?nbsp;鑷畾鍒?/span>
this.barManager1.AllowQuickCustomization = false;
this.LookAndFeel.UseDefaultLookAndFeel = true;
//娣誨姞涓涓狟arSubItem DevExpress.XtraBars.BarSubItem mPaintStyle;
//娣誨姞鎹㈣偆
SkinHelper.InitSkinPopupMenu(mPaintStyle);
//鏍囬鏍忔敮鎸佺毊鑲?/span>
DevExpress.Skins.SkinManager.EnableFormSkins();
}
}
]]>
explicit錛圕# 鍙傝冿級
explicit 鍏抽敭瀛楃敤浜庡0鏄庡繀欏諱嬌鐢ㄥ己鍒惰漿鎹㈡潵璋冪敤鐨勭敤鎴峰畾涔夌殑綾誨瀷杞崲榪愮畻絎︺?nbsp;渚嬪錛屽湪涓嬮潰鐨勭ず渚嬩腑錛屾榪愮畻絎﹀皢鍚嶄負 Fahrenheit 鐨勭被杞崲涓哄悕涓?nbsp;Celsius 鐨勭被錛?br />// Must be defined inside a class called Fahrenheit:
public static explicit operator Celsius(Fahrenheit fahr)
{
return new Celsius((5.0f / 9.0f) * (fahr.degrees - 32));
}
鍙互濡備笅鎵紺鴻皟鐢ㄦ杞崲榪愮畻絎︼細
Fahrenheit fahr = new Fahrenheit(100.0f);
Console.Write("{0} Fahrenheit", fahr.Degrees);
Celsius c = (Celsius)fahr;
杞崲榪愮畻絎﹀皢婧愮被鍨嬭漿鎹負鐩爣綾誨瀷銆?nbsp;婧愮被鍨嬫彁渚涜漿鎹㈣繍綆楃銆?nbsp;涓庨殣寮忚漿鎹笉鍚岋紝蹇呴』閫氳繃寮哄埗杞崲鐨勬柟寮忔潵璋冪敤鏄懼紡杞崲榪愮畻絎︺?nbsp;濡傛灉杞崲鎿嶄綔鍙兘瀵艱嚧寮傚父鎴栦涪澶變俊鎭紝鍒欏簲灝嗗叾鏍囪涓?nbsp;explicit銆?nbsp;榪欏彲浠ラ槻姝㈢紪璇戝櫒鏃犳彁紺哄湴璋冪敤鍙兘浜х敓鏃犳硶棰勮鍚庢灉鐨勮漿鎹㈡搷浣溿?br />鐪佺暐姝ゅ己鍒惰漿鎹㈠皢瀵艱嚧緙栬瘧鏃墮敊璇?nbsp;CS0266銆?br />
implicit錛圕# 鍙傝冿級
implicit 鍏抽敭瀛楃敤浜庡0鏄庨殣寮忕殑鐢ㄦ埛瀹氫箟綾誨瀷杞崲榪愮畻絎︺?nbsp;濡傛灉鍙互紜繚杞崲榪囩▼涓嶄細閫犳垚鏁版嵁涓㈠け錛屽垯鍙嬌鐢ㄨ鍏抽敭瀛楀湪鐢ㄦ埛瀹氫箟綾誨瀷鍜屽叾浠栫被鍨嬩箣闂磋繘琛岄殣寮忚漿鎹€?br />闅愬紡杞崲鍙互閫氳繃娑堥櫎涓嶅繀瑕佺殑綾誨瀷杞崲鏉ユ彁楂樻簮浠g爜鐨勫彲璇繪с?nbsp;浣嗘槸錛屽洜涓洪殣寮忚漿鎹笉闇瑕佺▼搴忓憳灝嗕竴縐嶇被鍨嬫樉寮忓己鍒惰漿鎹負鍙︿竴縐嶇被鍨嬶紝鎵浠ヤ嬌鐢ㄩ殣寮忚漿鎹㈡椂蹇呴』鏍煎灝忓績錛屼互鍏嶅嚭鐜版剰澶栫粨鏋溿?nbsp;涓鑸儏鍐典笅錛岄殣寮忚漿鎹㈣繍綆楃搴斿綋浠庝笉寮曞彂寮傚父騫朵笖浠庝笉涓㈠け淇℃伅錛屼互渚垮彲浠ュ湪紼嬪簭鍛樹笉鐭ユ檽鐨勬儏鍐典笅瀹夊叏浣跨敤瀹冧滑銆?nbsp;濡傛灉杞崲榪愮畻絎︿笉鑳芥弧瓚抽偅浜涙潯浠訛紝鍒欏簲灝嗗叾鏍囪涓?nbsp;explicit銆?nbsp;
class Digit
{
public Digit(double d) { val = d; }
public double val;
// other members
// User-defined conversion from Digit to double
public static implicit operator double(Digit d)
{
return d.val;
}
// User-defined conversion from double to Digit
public static implicit operator Digit(double d)
{
return new Digit(d);
}
}
class Program
{
static void Main(string[] args)
{
Digit dig = new Digit(7);
//This call invokes the implicit "double" operator
double num = dig;
//This call invokes the implicit "Digit" operator
Digit dig2 = 12;
Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
Console.ReadLine();
}
}
]]>
]]>
杞嚜:
http://blog.csdn.net/hi_kong/article/details/46356801
鍦╒S2010涓柊寤洪」鐩紝棣栧厛閫夋嫨.net framework2.0錛岀劧鍚庡啀閫夋嫨web鏈嶅姟搴旂敤紼嬪簭銆傚鍥炬墍紺猴細
鐒跺悗鍦⊿ervice.cs鏂囦歡涓坊鍔犺嚜宸辯殑鏂規(guī)硶鍗沖彲銆?br />浜屻丆#winform璋冪敤webservice鏈嶅姟
鏂板緩C#winfrom宸ョ▼錛屽彸閿紩鐢?/span>-銆嬫坊鍔犳湇鍔″紩鐢?/span>-銆嬮珮綰?/span>-銆嬫坊鍔爓eb寮曠敤錛屽湪URL鍚庢坊鍔犻渶瑕佽繛鎺ョ殑url錛堣繍琛屼笂闈eb鏈嶅姟紼嬪簭嫻忚鍣ㄤ腑灝變細鍑虹幇榪炴帴鐨剈rl錛夛紝鐒跺悗鐐瑰嚮鍚庨潰鐨勭澶達紝鍐嶇偣娣誨姞寮曠敤鍗沖彲銆?br />鍋囪寮曠敤鐨勬湇鍔″悕涓篧ebReference錛岀被鍚嶇О涓篠ervice錛?br />using System.Web.Services;
using System.Web.Services.Description;
鍦ㄤ唬鐮佷腑濡備笅鎿嶄綔錛?br />
WebReference.Service1 helloWord = new WebReference.Service1();
helloWord .Url =WebUrl;//璁劇疆url
helloWord.helloWord錛堬級;//璋冪敤webservice涓殑鏂規(guī)硶
緇撴潫銆?br />
WebService澶勭悊澶ф暟鎹噺鏁版嵁
鍦ㄩ氳繃WebService澶勭悊澶ф暟鎹噺鏁版嵁鏃跺嚭鐜板涓嬮敊璇細
soap fault: 榪愯閰嶇疆鏂囦歡涓寚瀹氱殑鎵╁睍鏃跺嚭鐜板紓甯搞?nbsp;---> 瓚呰繃浜嗘渶澶ц姹傞暱搴︺?nbsp;
瑙e喅鏂規(guī)硶錛?br />鍥犱負涓婁紶鐨勬枃浠跺ぇ浜庣郴緇熼粯璁ら厤緗殑鍊鹼紝asp.net web service榛樿鐨勮姹傞暱搴︽槸4M銆?br />1銆侀拡瀵瑰崟涓」鐩紝鍙渶淇敼Web.config灝卞彲浠ヤ簡錛?br />淇敼閰嶇疆鍙互鍦╳eb.config涓噸鏂拌緗紝濡備笅錛?
<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
</system.web>
</configuration>
]]>
using System.ServiceModel; //娣誨姞System.ServiceModel寮曠敤
using System.ServiceModel.Channels;
namespace ConsoleApplication1
{
[ServiceContract]
public interface IHelloWCF
{
[OperationContract]
void Say(string input);
}
public sealed class HelloService : IHelloWCF
{
public void Say(string input)
{
Console.WriteLine("Hello {0}", input);
}
}
class Program
{
static void Main(string[] args)
{
Uri address = new Uri("http://127.0.0.1:8733/IHelloWCF");
BasicHttpBinding binding = new BasicHttpBinding();
ServiceHost svc = new ServiceHost(typeof(HelloService));
svc.AddServiceEndpoint(typeof(IHelloWCF), binding, address);
svc.Opened += delegate {
Console.WriteLine("The HelloWCF receiving application is ready");
};
svc.Open();
ChannelFactory<IHelloWCF> factory = new ChannelFactory<IHelloWCF>(binding,new EndpointAddress(address));
IHelloWCF proxy = factory.CreateChannel();
proxy.Say("寮犱笁");
proxy.Say("鏉庡洓");
proxy.Say("鐜嬩簩楹誨瓙");
Console.ReadLine();
svc.Close();
}
}
}
]]>
涓銆佸厓鏁版嵁鍗砏SDL錛屾弿榪頒簡鏈嶅姟鐨勭粏鑺傦紝浠ヤ究瀹㈡埛绔嬌鐢ㄣ?br />浜屻佸繀欏諱負鏈嶅姟閰嶇疆ServiceMetadata琛屼負錛屾墠鑳戒負鍏剁敓鎴怶SDL錛屾墠鑳藉啀浣跨敤httpGetEnabled鎴杕ex灝嗗叾鍏竷鍑哄幓
涓夈佽繖涓ょ鏂瑰紡鍏竷鍑哄幓鐨刉SDL鏃犲尯鍒備絾鍏竷鐨勬柟寮忔湁鍖哄埆
1銆乭ttpGetEnabled=true錛岀被浼肩殑榪樻湁httpsGetEnabled=true
姝ゆ柟寮忛氳繃鍦ㄦ湇鍔″湪鐨刄RL鍚庡姞“?wsdl”鐨勬柟寮忓叕甯僕SDL錛屽彲鐩存帴閫氳繃HTTP璁塊棶寰楀埌銆?br />
2銆乵ex
姝ゆ柟寮忎互涓鑸殑緇堢粨鐐規(guī)柟寮忓叕甯冿紝鏀寔鍚勭鍗忚錛歨ttp銆乼cp銆丯amedPipe
]]>
public class UserInfo
{
//….
}
鍙湁澹版槑涓篋ataContract鐨勭被鍨嬬殑瀵硅薄鍙互琚紶閫侊紝涓斿彧鏈夋垚鍛樺睘鎬т細琚紶閫掞紝鎴愬憳鏂規(guī)硶涓嶄細琚紶閫掋俉CF瀵瑰0鏄庝負DataContract鐨勭被鍨嬫彁渚涙洿鍔犵粏鑺傜殑鎺у埗錛屽彲浠ユ妸涓涓垚鍛樻帓闄ゅ湪搴忓垪鍖栬寖鍥翠互澶栵紝涔熷氨鏄錛屽鎴風紼嬪簭涓嶄細鑾峰緱琚帓闄ゅ湪澶栫殑鎴愬憳鐨勪換浣曚俊鎭紝鍖呮嫭瀹氫箟鍜屾暟鎹傞粯璁ゆ儏鍐典笅錛屾墍鏈夌殑鎴愬憳灞炴ч兘琚帓闄ゅ湪澶栵紝鍥犳闇瑕佹妸姣忎竴涓浼犻佺殑鎴愬憳澹版槑涓篋ataMember錛屽涓嬫墍紺恒?br /> [DataContract]
public class UserInfo
{
[DataMember]
public string UserName
{
get;
set;
}
[DataMember]
public int Age
{
get;
set;
}
[DataMember]
public string Location
{
get;
set;
}
public string Zodiac
{
get;
set;
}
}
涓婇潰榪欐浠g爜鎶奤serInfo綾誨0鏄庝負DataContract錛屽皢UserName銆丄ge銆丩ocation榪?涓睘鎬у0鏄庝負DataMember錛堟暟鎹垚鍛橈級銆俍odiac鎴愬憳娌℃湁琚0鏄庝負DataMember錛屽洜姝ゅ湪浜ゆ崲鏁版嵁鏃訛紝涓嶄細浼犺緭Zodiac鐨勪換浣曚俊鎭?br />DataContract涔熸敮鎸丯ame/Namespace灞炴э紝濡傚悓ServiceContract錛孨ame鍜孨amespace鍙互鑷畾涔夊悕縐板拰鍛藉悕絀洪棿錛屽鎴風灝嗕嬌鐢ㄨ嚜瀹氫箟鐨勫悕縐板拰鍛藉悕絀洪棿瀵笵ataContract綾誨瀷榪涜璁塊棶銆?br />澹版槑涓篋ataMember鐨勬垚鍛樹篃鍙互鑷畾涔夊鎴風鍙鐨勫悕縐幫紝渚嬪錛?br />[DataMember(Name="Name")]
public string UserName
{
get;
set;
}
[DataMember(Name="Age")]
public int UserAge
{
get;
set;
}
闄や簡Name鍜孨amespace浠ュ錛孌ataMember榪樻湁浠ヤ笅鍙傛暟錛屽畠浠殑鍚箟鍒嗗埆濡備笅銆?br />錛?/span>1錛塈sRequired錛氬間負true鏃訛紝瑕佹眰搴忓垪鍖栧紩鎿庢鏌ュ璞℃槸鍚﹀瓨鍦ㄨ鍊鹼紱鑻ユ棤錛屽垯浼氭湁寮傚父鎶涘嚭銆?br />錛?/span>2錛塐rder錛歜ool綾誨瀷鍊鹼紝鍊間負true鏃訛紝搴忓垪鍖栧拰鍙嶅簭鍒楀寲榪囩▼灝嗕細鎸夋垚鍛樺畾涔夌殑欏哄簭榪涜錛岃繖瀵逛緷璧栦簬鎴愬憳浣嶇疆鐨勫弽搴忓垪鍖栬繃紼嬫棤姣旈噸瑕併?br />錛?/span>3錛塃mitDefaultvalue錛氫負鎴愬憳灞炴ц緗竴涓粯璁ゅ箋?/span>
]]>
#include <msclr/marshal.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;
#ifdef _MANAGED
#endif
void abc123()
{
using namespace msclr::interop;
const char* msg1 = "Test String to Marshal";
const wchar_t* msg2= L"Good Luck Aaron";
String^ aMsg = marshal_as<String^>(msg1);
String^ uMsg = marshal_as<String^>(msg2);
Console::WriteLine(aMsg);
Console::WriteLine(uMsg);
}
#pragma unmanaged
int _tmain(int argc, _TCHAR* argv[])
{
abc123();
system("pause");
return 0;
}
#pragma managed
]]>
鎴戜滑鍦ㄤ嬌鐢╟#鎵樼浠g爜鏃訛紝鍐呭瓨鍦板潃鍜孏C鍥炴敹閭d笉鏄垜浠叧蹇冪殑錛孋LR宸茬粡緇欐垜浠殫綆辨搷浣溿?br />浣嗘槸濡傛灉鎴戜滑鍦╟#涓皟鐢ㄤ簡涓涓潪鎵樼浠g爜錛屾瘮濡倂c鐨凞LL,鑰屼笖浠栨湁涓洖璋冨嚱鏁幫紝闇瑕佸紩鐢╟#涓殑鏌愪釜瀵硅薄騫舵搷浣滐紝
榪欐椂鍊欎綘灝卞緱瑕佸皬蹇冧簡銆?br />瑕佹槸闈炴墭綆′唬鐮佷腑鐢ㄥ埌寰楁墭綆′唬鐮侀偅涓璞¤GC緇欏洖鏀朵簡錛岃繖鏃跺欏氨浼氭姤鍐呭瓨閿欒銆?br />鎵浠ユ垜浠氨瑕佹妸閭d釜瀵硅薄“閽?#8221;浣?pin)錛岃瀹冪殑鍐呭瓨鍦板潃鍥哄畾錛岃屼笉琚瀮鍦懼洖鏀舵帀錛岀劧鍚庢渶鍚庢垜浠嚜宸辯鐞嗭紝鑷繁閲婃斁鍐呭瓨,榪欐椂鍊欏氨闇瑕丟CHandle,鏉ョ湅涓猰sdn涓婄殑渚嬪瓙:
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
//C#
public delegate bool CallBack(int handle, IntPtr param);
public class LibWrap
{
[DllImport("user32.dll")]
public static extern bool EnumWindows(CallBack cb, IntPtr param);
}
class Program
{
static void Main(string[] args)
{
TextWriter tw = System.Console.Out;
GCHandle gch = GCHandle.Alloc(tw);
CallBack cewp = new CallBack(CaptureEnumWindowsProc);
LibWrap.EnumWindows(cewp, (IntPtr)gch);
gch.Free();
Console.Read();
}
private static bool CaptureEnumWindowsProc(int handle, IntPtr param)
{
GCHandle gch = (GCHandle)param;
TextWriter tw = (TextWriter)gch.Target;
tw.WriteLine(handle);
return true;
}
}
}
瀵逛笂闈㈢殑浠g爜錛岀暐鍔犺В閲婏細gch 浼氶拤浣?pin)tw榪欎釜瀵硅薄錛屼嬌鍏朵笉鍙桮C綆$悊錛屽憡璇夊畠錛屼互鍚庝綘宕╃鎴戯紝鎴戜篃涓嶇敤緇欎綘涓婄◣,鍏跺疄綆$悊鏉冨凡緇忕粰gch錛岄氳繃free鏉ラ噴鏀懼唴瀛樸?br />榪欑鎯呭喌涓昏鐢ㄥ湪鎵樼鍜岄潪鎵樼浠g爜浜や簰鐨勬椂鍊欙紝闃叉鍐呭瓨娉勯湶鏉ヤ嬌鐢℅CHandle銆?/div>
]]>
閽変綇鎸囬拡
閫氬父錛屽湪鍘嬬緝闃舵錛屽瀮鍦懼洖鏀跺櫒鍙夋嫨灝嗛┗鐣欏湪 CLR 鍫嗕笂鐨勫璞$Щ鍔ㄥ埌鍫嗗唴鐨勪笉鍚屼綅緗傝繖縐嶇Щ鍔ㄥ浜庤窡韙彞鏌勩佽窡韙紩鐢ㄥ拰鍐呴儴鎸囬拡鑰岃█涓嶆槸闂錛屽畠浠兘閫忔槑鍦版洿鏂拌繖浜涘疄浣撱備絾鏄紝濡傛灉鐢ㄦ埛鍦ㄨ繍琛屾椂鐜澶栦紶閫掍簡 CLR 鍫嗕笂瀵硅薄鐨勫湴鍧錛岃繖縐嶇Щ鍔ㄥ氨浼氬鑷撮棶棰樸傚湪榪欑鎯呭喌涓嬶紝瀵硅薄鐨勪笉紼沖畾榪愬姩鍙兘瀵艱嚧榪愯鏃跺け璐ャ傝閬垮厤姝ょ被瀵硅薄鐨勭Щ鍔紝蹇呴』鍦ㄦ湰鍦板皢瀹冧滑閽夊湪鑷繁鐨勪綅緗笂浠ヤ緵澶栭儴浣跨敤銆?br />
鍦ㄦ墭綆℃墿灞曚腑錛?#8220;閽変綇鎸囬拡”鏄氳繃浣跨敤 __pin 鍏抽敭瀛楅檺瀹氭寚閽堝0鏄庢潵澹版槑鐨勩備笅闈㈡槸涓涓潵鑷墭綆℃墿灞曡鑼冪殑緇忚繃鐣ュ井淇敼鐨勭ず渚嬶細
澶嶅埗浠g爜
__gc struct H { int j; };
int main()
{
H * h = new H;
int __pin * k = & h -> j;
// …
};
鍦ㄦ柊鐨勮璦璁捐涓紝閽変綇鎸囬拡鏄嬌鐢ㄧ被浼間簬鍐呴儴鎸囬拡鐨勮娉曟潵澹版槑鐨勩?br />
澶嶅埗浠g爜
ref struct H
{
public:
int j;
};
int main()
{
H^ h = gcnew H;
pin_ptr<int> k = &h->j;
// …
}
interior_ptr
Declares a pointer to inside a reference type, but not to the object itself. An interior pointer can point to a reference handle, value type, boxed type handle, member of a managed type, or to an element of a managed array.
cli::interior_ptr<cv_qualifier type> var = &initializer;
Remarks
A native pointer is not able to track an item as its location changes on the managed heap, which results from the garbage collector moving instances of an object. In order for a pointer to correctly refer to the instance, the runtime needs to update the pointer to the newly positioned object.
An interior_ptr represents a superset of the functionality of a native pointer. Therefore, anything that can be assigned to a native pointer can also be assigned to an interior_ptr. An interior pointer is permitted to perform the same set of operations as native pointers, including comparison and pointer arithmetic.
An interior pointer can only be declared on the stack. An interior pointer cannot be declared as a member of a class.
Since interior pointers exist only on the stack, taking the address of an interior pointer yields an unmanaged pointer.
interior_ptr has an implicit conversion to bool, which allows for its use in conditional statements.
For information on how to declare an interior pointer that points into an object that cannot be moved on the garbage-collected heap, see pin_ptr.
interior_ptr is in the cli namespace. See cli Namespace for more information.
//Example
// interior_ptr.cpp
// compile with: /clr
using namespace System;
ref class MyClass {
public:
int data;
};
int main() {
MyClass ^ h_MyClass = gcnew MyClass;
h_MyClass->data = 1;
Console::WriteLine(h_MyClass->data);
interior_ptr<int> p = &(h_MyClass->data);
*p = 2;
Console::WriteLine(h_MyClass->data);
// alternatively
interior_ptr<MyClass ^> p2 = &h_MyClass;
(*p2)->data = 3;
Console::WriteLine((*p2)->data);
}
]]>
鍏叡璇█榪愯搴撳埄鐢⊿tructLayoutAttribute鎺у埗綾繪垨緇撴瀯鐨勬暟鎹瓧孌靛湪鎵樼鍐呭瓨涓殑鐗╃悊甯冨眬,鍗崇被鎴栫粨鏋勯渶瑕佹寜鏌愮鏂瑰紡鎺掑垪銆傚鏋滆灝嗙被浼犻掔粰闇瑕佹寚瀹氬竷灞鐨勯潪鎵樼浠g爜錛屽垯鏄懼紡鎺у埗綾誨竷灞鏄噸瑕佺殑銆傚畠鐨勬瀯閫犲嚱鏁頒腑鐢?nbsp;LayoutKind鍊煎垵濮嬪寲 StructLayoutAttribute 綾葷殑鏂板疄渚嬨?nbsp;LayoutKind.Sequential 鐢ㄤ簬寮哄埗灝嗘垚鍛樻寜鍏跺嚭鐜扮殑欏哄簭榪涜欏哄簭甯冨眬銆?br />
StructLayout鐗規(guī)у厑璁告垜浠帶鍒禨tructure璇彞鍧楃殑鍏冪礌鍦ㄥ唴瀛樹腑鐨勬帓鍒楁柟寮?浠ュ強褰撹繖浜涘厓绱犺浼犻掔粰澶栭儴DLL鏃訛紝榪愯搴撴帓鍒楄繖浜涘厓绱犵殑鏂瑰紡銆俈isual Basic緇撴瀯鐨勬垚鍛樺湪鍐呭瓨涓殑欏哄簭鏄寜鐓у畠浠嚭鐜板湪婧愪唬鐮佷腑鐨勯『搴忔帓鍒楃殑錛屽敖綆$紪璇戝櫒鍙互鑷敱鐨勬彃鍏ュ~鍏呭瓧鑺傛潵瀹夋帓榪欎簺鎴愬憳錛屼互渚夸嬌寰?6浣嶆暟鍊肩敤瀛愯竟鐣屽榻愶紝32浣嶆暟鍊肩敤鍙屽瓧杈圭晫瀵歸綈銆?nbsp;
浣跨敤榪欑鎺掑垪錛堟湭鍘嬬緝甯冨眬錛夋彁渚涚殑鎬ц兘鏈浣熾?nbsp;
閫氳繃System.Runtime.InteropServices.StructLayout 鐗規(guī)х簿紜殑鎺у埗姣忎竴涓粨鏋勬垚鍛樼殑浣嶇疆銆?br />System.Runtime.InteropServices.StructLayout 鍏佽鐨勫兼湁:
StructLayout.Auto
StructLayout.Sequential
StructLayout.Explicit
1.Sequential,欏哄簭甯冨眬,姣斿
struct S1
{
int a;
int b;
}
閭d箞榛樿鎯呭喌涓嬪湪鍐呭瓨閲屾槸鍏堟帓a,鍐嶆帓b
涔熷氨鏄鏋滆兘鍙栧埌a鐨勫湴鍧,鍜宐鐨勫湴鍧,鍒欑浉宸竴涓猧nt綾誨瀷鐨勯暱搴?4瀛楄妭
[StructLayout(LayoutKind.Sequential)]
struct S1
{
int a;
int b;
}
榪欐牱鍜屼笂涓涓槸涓鏍風殑.鍥犱負榛樿鐨勫唴瀛樻帓鍒楀氨鏄疭equential,涔熷氨鏄寜鎴愬憳鐨勫厛鍚庨『搴忔帓鍒?
2.Explicit,綺劇‘甯冨眬
闇瑕佺敤FieldOffset()璁劇疆姣忎釜鎴愬憳鐨勪綅緗?br />榪欐牱灝卞彲浠ュ疄鐜扮被浼糲鐨勫叕鐢ㄤ綋(union)鐨勫姛鑳?br />[StructLayout(LayoutKind.Explicit)]
struct S1
{
[FieldOffset(0)] int a;
[FieldOffset(0)] int b;
}
榪欐牱a鍜宐鍦ㄥ唴瀛樹腑鍦板潃鐩稿悓
StructLayout鐗規(guī)ф敮鎸佷笁縐嶉檮鍔犲瓧孌碉細CharSet銆丳ack銆丼ize銆?nbsp;
· CharSet瀹氫箟鍦ㄧ粨鏋勪腑鐨勫瓧絎︿覆鎴愬憳鍦ㄧ粨鏋勮浼犵粰DLL鏃剁殑鎺掑垪鏂瑰紡銆傚彲浠ユ槸Unicode銆丄nsi鎴朅uto銆?nbsp;
榛樿涓篈uto錛屽湪WIN NT/2000/XP涓〃紺哄瓧絎︿覆鎸夌収Unicode瀛楃涓茶繘琛屾帓鍒楋紝鍦╓IN 95/98/Me涓垯琛ㄧず鎸夌収ANSI瀛楃涓茶繘琛屾帓鍒椼?nbsp;
· Pack瀹氫箟浜嗙粨鏋勭殑灝佽澶у皬銆傚彲浠ユ槸1銆?銆?銆?銆?6銆?2銆?4銆?28鎴栫壒孌婂?銆傜壒孌婂?琛ㄧず褰撳墠鎿嶄綔騫沖彴榛樿鐨勫帇緙╁ぇ灝忋?nbsp;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct LIST_OPEN
{
public int dwServerId;
public int dwListId;
public System.UInt16 wRecordSize;
public System.UInt16 wDummy;
public int dwFileSize;
public int dwTotalRecs;
public NS_PREFETCHLIST sPrefetch;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)] public string szSrcMach;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)] public string szSrcComp;
}
姝や緥涓敤鍒癕ashalAs鐗規(guī)э紝瀹冪敤浜庢弿榪板瓧孌點佹柟娉曟垨鍙傛暟鐨勫皝閫佸鐞嗘牸寮忋傜敤瀹冧綔涓哄弬鏁板墠緙騫舵寚瀹氱洰鏍囬渶瑕佺殑鏁版嵁綾誨瀷銆?br />渚嬪錛屼互涓嬩唬鐮佸皢涓や釜鍙傛暟浣滀負鏁版嵁綾誨瀷闀挎寚閽堝皝閫佺粰 Windows API 鍑芥暟鐨勫瓧絎︿覆 (LPStr)錛?nbsp;
[MarshalAs(UnmanagedType.LPStr)] String existingfile;
[MarshalAs(UnmanagedType.LPStr)] String newfile;
娉ㄦ剰緇撴瀯浣滀負鍙傛暟鏃跺欙紝涓鑸墠闈㈣鍔犱笂ref淇グ絎︼紝鍚﹀垯浼氬嚭鐜伴敊璇細瀵硅薄鐨勫紩鐢ㄦ病鏈夋寚瀹氬璞$殑瀹炰緥銆?br />[ DllImport( "kernel32", EntryPoint="GetVersionEx" )]
public static extern bool GetVersionEx2( ref OSVersionInfo2 osvi );
string result;
IntPtr strPtr = new IntPtr() ;// unmanaged api TestStringAsResultIntPrt(1);
result = Marshal.PtrToStringUni(strPtr);
result = Marshal.PtrToStringAnsi(strPtr);
Marshal.FreeCoTaskMem(strPtr);
]]>
// convert_system_string.cpp
// compile with: /clr
#include <string>
#include <iostream>
using namespace std;
using namespace System;
void MarshalString ( String ^ s, string& os ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
void MarshalString ( String ^ s, wstring& os ) {
using namespace Runtime::InteropServices;
const wchar_t* chars =
(const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
int main() {
string a = "test";
wstring b = L"test2";
String ^ c = gcnew String("abcd");
cout << a << endl;
MarshalString(c, a);
c = "efgh";
MarshalString(c, b);
cout << a << endl;
wcout << b << endl;
}
]]>
1錛夌敤C#鍐欎換浣曠殑綾誨簱錛屽錛歂eoWayMesCommon.dll
2錛塁++鐨刣ll涓寮曠敤姝ょ被搴擄紝娉ㄦ剰涓嶈兘鍦╯tdafx.h涓紩鐢紝鏈鍚庢槸鍦╟pp鏂囦歡涓紩鐢ㄣ?br />#using <mscorlib.dll>
#using "../Debug/NeoWayMesCommon.dll" //寮曠敤姝ょ被搴?/span>
using namespace NeoWayMes;
using namespace System;
3) C++ 緙栬瘧璁劇疆涓瀹氳緗負錛氭敮鎸佸叕鍏辮璦榪愯鏃舵敮鎸?/clr)
4) 蹇呴』灝咰#鐨刣ll鎷瘋礉鑷砎C宸ョ▼榪愯鐨勭洰褰曪紝鍚﹀垯鎶ュ紓甯擱敊璇?br />5) 鍏朵粬鐨勫弬瑙乧++/cli璇硶鏍囧噯
]]>
By default, when compiling with /clr (not /clr:pure), the definition of a managed function causes the compiler to generate a managed entry point and a native
c++ cli 鏍囧噯
]]>
// compile with: /clr
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
#pragma unmanaged
void NativeTakesAString(const char* p) {
printf_s("(native) received '%s'\n", p);
}
#pragma managed
int main() {
String^ s = gcnew String("sample string");
IntPtr ip = Marshal::StringToHGlobalAnsi(s);
const char* str = static_cast<const char*>(ip.ToPointer());
Console::WriteLine("(managed) passing string");
NativeTakesAString( str );
Marshal::FreeHGlobal( ip );
}
涓嬮潰鐨勭ず渚嬫紨紺鴻闂敱闈炴墭綆″嚱鏁拌皟鐢ㄧ殑鎵樼鍑芥暟涓殑 ANSI 瀛楃涓叉墍闇鐨勬暟鎹皝閫佸鐞嗐傛墭綆″嚱鏁板湪鎺ユ敹鍒版湰鏈哄瓧絎︿覆鏃訛紝鍙互鐩存帴浣跨敤璇ュ瓧絎︿覆錛屼篃鍙互鎸夋墍紺烘柟寮忎嬌鐢?nbsp;PtrToStringAnsi 鏂規(guī)硶灝嗗叾杞崲涓烘墭綆″瓧絎︿覆銆?br />
澶嶅埗浠g爜
// MarshalANSI2.cpp
// compile with: /clr
#include <iostream>
#include <vcclr.h>
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
#pragma managed
void ManagedStringFunc(char* s) {
String^ ms = Marshal::PtrToStringAnsi(static_cast<IntPtr>(s));
Console::WriteLine("(managed): received '{0}'", ms);
}
#pragma unmanaged
void NativeProvidesAString() {
cout << "(native) calling managed func\n";
ManagedStringFunc("test string");
}
#pragma managed
int main() {
NativeProvidesAString();
}
]]>
// compile with: /clr
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >
using namespace System;
int main() {
String ^str = "Hello";
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_t> wch = PtrToStringChars(str);
printf_s("%S\n", wch);
// Conversion to char* :
// Can just convert wchar_t* to char* using one of the
// conversion functions such as:
// WideCharToMultiByte()
// wcstombs_s()
// etc
size_t convertedChars = 0;
size_t sizeInBytes = ((str->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,
ch, sizeInBytes,
wch, sizeInBytes);
if (err != 0)
printf_s("wcstombs_s failed!\n");
printf_s("%s\n", ch);
}
]]>
1.using鎸囦護銆?span style="color: #0000FF; ">using + 鍛藉悕絀洪棿鍚嶅瓧錛岃繖鏍峰彲浠ュ湪紼嬪簭涓洿鎺ョ敤鍛戒護絀洪棿涓殑綾誨瀷錛岃屼笉蹇呮寚瀹氱被鍨嬬殑璇︾粏鍛藉悕絀洪棿錛岀被浼間簬Java鐨刬mport錛岃繖涓姛鑳戒篃鏄渶甯哥敤鐨勶紝鍑犱箮姣忎釜cs鐨勭▼搴忛兘浼氱敤鍒般?nbsp;
渚嬪錛?span style="color: #0000FF; ">using System; 涓鑸兘浼氬嚭鐜板湪*.cs涓?br />
2.using鍒悕銆?span style="color: #0000FF; ">using + 鍒悕 = 鍖呮嫭璇︾粏鍛藉悕絀洪棿淇℃伅鐨勫叿浣撶殑綾誨瀷銆?nbsp;
榪欑鍋氭硶鏈変釜濂藉灝辨槸褰撳悓涓涓猚s寮曠敤浜嗕袱涓笉鍚岀殑鍛藉悕絀洪棿錛屼絾涓や釜鍛藉悕絀洪棿閮藉寘鎷簡涓涓浉鍚屽悕瀛楃殑綾誨瀷鐨勬椂鍊欍傚綋闇瑕佺敤鍒拌繖涓被鍨嬬殑鏃跺欙紝灝辨瘡涓湴鏂歸兘瑕佺敤璇︾粏鍛藉悕絀洪棿鐨勫姙娉曟潵鍖哄垎榪欎簺鐩稿悓鍚嶅瓧鐨勭被鍨嬨傝岀敤鍒悕鐨勬柟娉曚細鏇寸畝媧侊紝鐢ㄥ埌鍝釜綾誨氨緇欏摢涓被鍋氬埆鍚嶅0鏄庡氨鍙互浜嗐傛敞鎰忥細騫朵笉鏄涓や釜鍚嶅瓧閲嶅錛岀粰鍏朵腑涓涓敤浜嗗埆鍚嶏紝鍙﹀涓涓氨涓嶉渶瑕佺敤鍒悕浜嗭紝濡傛灉涓や釜閮借浣跨敤錛屽垯涓や釜閮介渶瑕佺敤using鏉ュ畾涔夊埆鍚嶇殑銆?br />using Row = Newtonsoft.Json.Linq.JObject;
using Rows = Newtonsoft.Json.Linq.JArray;
3.using璇彞錛屽畾涔変竴涓寖鍥達紝鍦ㄨ寖鍥寸粨鏉熸椂澶勭悊瀵硅薄銆?nbsp;
鍦烘櫙錛?nbsp;
褰撳湪鏌愪釜浠g爜孌典腑浣跨敤浜嗙被鐨勫疄渚嬶紝鑰屽笇鏈涙棤璁哄洜涓轟粈涔堝師鍥狅紝鍙紱誨紑浜嗚繖涓唬鐮佹灝辮嚜鍔ㄨ皟鐢ㄨ繖涓被瀹炰緥鐨凞ispose銆?nbsp;
瑕佽揪鍒拌繖鏍風殑鐩殑錛岀敤trycatch鏉ユ崟鎹夊紓甯鎬篃鏄彲浠ョ殑錛屼絾鐢╱sing涔熷緢鏂逛究銆?br />
using (Class1 cls1 = new Class1(), cls2 = new Class1())
{
// the code using cls1, cls2
} // call
]]>
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Xml;
using Row = System.Collections.Generic.Dictionary<string, string>;
namespace NeowayFixPlatform
{
class MysqlFunction
{
public static string table_fix = "nw_fix_record";
public bool IsConnected()
{
return (mysql.State == ConnectionState.Open);
}
public MysqlFunction()
{
if (mysql.State != ConnectionState.Open)
mysql.Open();
}
~MysqlFunction()
{
if (mysql.State == ConnectionState.Open)
mysql.Dispose();
}
private static String mysqlcon = GetConfigString();
MySqlConnection mysql = new MySqlConnection(mysqlcon);
public string quote(ref string value)
{
value = value.Replace(@"\", @"\\").Replace("'", @"\'");
return value;
}
public StringBuilder quote(ref StringBuilder value)
{
value = value.Replace(@"\", @"\\").Replace("'", @"\'");
return value;
}
public StringBuilder GetPairSQLString(Row row)
{
StringBuilder strSQL = new StringBuilder();
foreach (KeyValuePair<string, string> pair in row)
{
string value = pair.Value.ToString();
this.quote(ref value);
strSQL.AppendFormat("{0}='{1}',", pair.Key, value);
}
strSQL.Remove(strSQL.Length-1, 1);
return strSQL;
}
public bool UpdateOne(string table,Row row,string format,params object[] args)
{
StringBuilder condition = new StringBuilder();
condition.AppendFormat(format, args);
return this.UpdateOne(table, row, condition.ToString());
}
public bool UpdateOne(string strTable, Row row, string strQuery)
{
StringBuilder strSQL = new StringBuilder();
try
{
strSQL.AppendFormat("UPDATE {0} set {1} WHERE {2} LIMIT 1", strTable, GetPairSQLString(row), strQuery);
//mysql.Open();
MySqlCommand cmd = new MySqlCommand(strSQL.ToString(), mysql);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
public Row FetchOne(string strSQL)
{
Row row = new Row();
try
{
//mysql.Open();
MySqlCommand cmd = new MySqlCommand(strSQL.ToString(),mysql);
MySqlDataReader reader = cmd.ExecuteReader();
reader.Read();
for (int i = 0; i < reader.FieldCount; i++)
{
if (reader.HasRows)
row[reader.GetName(i)] = reader[i].ToString();
}
reader.Close();
cmd.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return row;
}
#region 鑾峰彇鐧婚檰瀛楃
/// <summary>
/// 鑾峰彇鐧婚檰瀛楃
/// </summary>
/// <returns></returns>
public static string GetConfigString()
{
string spath = Application.StartupPath;
spath += "\\config.xml";
XmlDocument xml = new XmlDocument();
xml.Load(spath);
XmlNode config = xml.SelectSingleNode("config");
XmlNode dbname = config.SelectSingleNode("dbname");
XmlNode ipaddr = config.SelectSingleNode("ipaddr");
XmlNode username = config.SelectSingleNode("username");
XmlNode password = config.SelectSingleNode("password");
return string.Format("database={0};Password={1};User ID={2};server={3}", dbname.InnerText, password.InnerText, username.InnerText, ipaddr.InnerText);
}
#endregion
public bool BindData(string sql,ref DataGridView dgv)
{
DataSet dataset = new DataSet();
DataTable datatable = new DataTable();
try
{
MySqlDataAdapter mdap = new MySqlDataAdapter(sql, mysql);
mdap.Fill(dataset);
datatable = dataset.Tables[0];
dgv.DataSource = datatable;
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
#region 鐢ㄦ埛鐧誨綍
/// <summary>
/// 緋葷粺鐧誨綍
/// </summary>
/// <returns></returns>
public bool Login(cUserInfo userinfo)
{
DataSet dataset = new DataSet();
DataTable datatable = new DataTable();
try
{
//MySqlDataAdapter 鏁版嵁搴撴墽琛屽嚱鏁幫紝鏌ヨ錛屾彃鍏ワ紝鍒犻櫎閮藉彲浣跨敤
MySqlDataAdapter mdap = new MySqlDataAdapter("select * from nw_user where user_name = '" + userinfo.User_name + "' and user_pwd = '" + userinfo.User_password + "'", mysql);
mdap.Fill(dataset);
datatable = dataset.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (datatable.Rows.Count == 1)
{
DataRow datarow = datatable.Rows[0];
userinfo.User_power = datarow["user_power"].ToString();
return true;
}
return false;
}
#endregion
public bool SaveFix(Dictionary<string,string> row,bool bNew)
{
StringBuilder strSQL = new StringBuilder();
try
{
if (bNew)
strSQL.Append("INSERT INTO nw_fix_record set ");
else
strSQL.Append("UPDATE nw_fix_record set ");
strSQL.AppendFormat("{0}",GetPairSQLString(row));
if (!bNew)
{
strSQL.AppendFormat(" WHERE IMEI_MEID='{0}' limit 1", row["IMEI_MEID"]);
}
//mysql.Open();
MySqlCommand cmd = new MySqlCommand(strSQL.ToString(), mysql);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
#region 鑾峰彇鎵鏈変駭鍝佸瀷鍙?br /> /// <summary>
/// 鑾峰彇鎵鏈変駭鍝佸瀷鍙?br /> /// </summary>
/// <returns></returns>
public string[] GetProducts()
{
DataSet dataset = new DataSet();
DataTable datatable = new DataTable();
string[] rows = null;
try
{
MySqlDataAdapter mdap = new MySqlDataAdapter("select * from nw_product where 1", mysql);
mdap.Fill(dataset);
datatable = dataset.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return rows;
}
int row_count = datatable.Rows.Count;
if (row_count>0)
{
rows = new string[row_count];
for (int i = 0; i < row_count; i++ )
{
rows[i] = datatable.Rows[i][0].ToString();
}
}
return rows;
}
#endregion
#region 鑾峰彇鎵鏈夊鎴峰悕縐?br /> /// <summary>
/// 鑾峰彇鎵鏈夊鎴峰悕縐?br /> /// </summary>
/// <returns></returns>
public string[] GetAllClientName()
{
DataSet dataset = new DataSet();
DataTable datatable = new DataTable();
string[] rows = null;
try
{
MySqlDataAdapter mdap = new MySqlDataAdapter("select client_name from nw_client where 1", mysql);
mdap.Fill(dataset);
datatable = dataset.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return rows;
}
int row_count = datatable.Rows.Count;
if (row_count > 0)
{
rows = new string[row_count];
for (int i = 0; i < row_count; i++)
{
rows[i] = datatable.Rows[i][0].ToString();
}
}
return rows;
}
#endregion
#region 鑾峰彇鎵鏈夌淮淇漢鍛?br /> /// <summary>
/// 鑾峰彇鎵鏈夌淮淇漢鍛?br /> /// </summary>
/// <returns></returns>
public string[] GetAllRepairEr()
{
DataSet dataset = new DataSet();
DataTable datatable = new DataTable();
string[] rows = null;
try
{
MySqlDataAdapter mdap = new MySqlDataAdapter("select user_name from nw_user where user_power='緇翠慨鍛? order by user_id", mysql);
mdap.Fill(dataset);
datatable = dataset.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return rows;
}
int row_count = datatable.Rows.Count;
if (row_count > 0)
{
rows = new string[row_count];
for (int i = 0; i < row_count; i++)
{
rows[i] = datatable.Rows[i][0].ToString();
}
}
return rows;
}
}
#endregion
#region 瀹氫箟鐢ㄦ埛琛?br /> public class cUserInfo
{
private int user_id;
public int User_id
{
get { return user_id; }
set { user_id = value; }
}
private string user_name;
public string User_name
{
get { return user_name; }
set { user_name = value; }
}
private string user_password;
public string User_password
{
get { return user_password; }
set { user_password = value; }
}
private string user_power;
public string User_power
{
get { return user_power; }
set { user_power = value; }
}
}
#endregion
}
]]>
GRANT ALL PRIVILEGES ON *.* TO test_user@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
libJson(Jsoncpp),澶熺敤浜?
C++:
extern "C" LIB_MESCLIENT_API int nw_save_prodmade_data(const char* jo_in);
Json::Value jo;
jo["row"]["product_id"] = "21265";
jo["row"]["product_sn"] = 223456789012345678;
int error_code = nw_save_prodmade_data(jo.toStyledString().c_str());
C#涓嬬敤Newtonsoft.Json涔熸尯濂界敤鐨?br />using Newtonsoft.Json.Linq;
JObject jo = new JObject();
jo["row"] = new JObject();
//姝ら儴鍒嗗湪dll鍐呴儴浼氭彃鍏ュ埌鍙拷婧姸鎬佽〃 nw_sn_status
jo["row"]["product_id"] = "12\r\n3\"12'\"65"; //浜у搧ID
StringBuilder row = new StringBuilder(jo.ToString());
int error_code = nw_save_prodmade_data(row);
]]>
杞嚜:http://blog.csdn.net/huxiangyang4/article/details/5853247