1.ws Դ?源码?
2.我把程序修改了 不知道怎样在ws程序下重读脚本
ws Դ??
呵呵,一年半前我和你一样,源码也处于这个状态,源码如果你做windows的源码程序设计的话,编程像windows这样的源码窗口化的软件,你必须接触VS,源码识别主力源码指标旗下的源码VC++或VC#,你学完c和c++先接触VC++的源码MFC较为容易,使用MFC APP向导可以直接生成你所说的源码windows这样窗口程序,刚开始你是源码不知道如何生成的,为此你需要边学windows程序设计,源码建议使用《windows程序设计》,源码里面介绍了一个基本窗体生成的源码原理和步骤。如果你要学VC#,源码那你得先学C#,源码sum函数法源码离做一个windows这样窗口程序比较远,而且也不知道它生成的原理,当然既然c和c++学的不错的话,c#入门也不难。如下是一个窗体生成的windows源码:
/*--------------------------------------
CLOCK.C -- Analog Clock Program
(c) Charles Petzold,
--------------------------------------*/
#include <windows.h>
#include <math.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Clock") ;
HWND hwnd;
MSG msg;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("GDI Test"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;
static PAINTSTRUCT ps;
static COLORREF color;
static UINT ixClient,iyClient;
static RECT rtWindow , rtClient;
switch (message)
{
case WM_CREATE :
GetWindowRect( hwnd , &rtWindow );
GetClientRect( hwnd , &rtClient );
return 0 ;
case WM_NCPAINT:
hdc = GetWindowDC( hwnd );
color = GetPixel( hdc , , 5 );
SetBkColor( hdc , color );
SetTextColor( hdc , RGB(,0,0) );
TextOut( hdc , , ,
"Editor : CM" , strlen("Editor : CM") );
ReleaseDC( hwnd , hdc );
return 0;
case WM_SIZE :
ixClient = LOWORD( wParam );
iyClient = HIWORD( wParam );
return 0 ;
case WM_TIMER :
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
TextOut( hdc , , , "Editor : CM" , strlen("Editor : CM") );
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage(0);
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
我把程序修改了 不知道怎样在ws程序下重读脚本
首先,动态编译实际上只涉及到两个类型:CodeDomProvider以及CompilerParameters他们都位于System.CodeDom.Compiler命名空间。
以下代码可将源码动态编译为一个程序集:
动态编译
获得assembly后,北美省钱快报源码随后我们即可以通过反射获取程序集里面的类型,然后实例化,调用类型方法
不过在此之前,我们得构造WS服务的代理类,使用WCF框架,创建服务代理类,常见的声明事务源码介绍代理类结构如下:
所以,我们要动态构造出代理类源码,应该知道服务的命名空间、服务方法的Action地址、ReplyAction地址,当然还有服务方法的名称,返回类型,参数列表。linux系统开源码这里,我们省略掉服务方法的参数列表,构造代理类,实际上就是一个字符串组装的问题,先创建一个类型,用于保存构造代理类所要用到的参数:
服务代理类构造参数
public class WebServiceParamaters
{
public string address;
public string Address
{
get { return address; }
set
{
address = value;
}
}
private string serviceNamespace;
public string ServiceNamespace
{
get { return serviceNamespace; }
set
{
serviceNamespace = value;
}
}
private string methodAction;
public string MethodAction
{
get { return methodAction; }
set
{
methodAction = value;
}
}
private string methodReplyAction;
public string MethodReplyAction
{
get { return methodReplyAction; }
set
{
methodReplyAction = value;
}
}
private string methodName;
public string MethodName
{
get { return methodName; }
set
{
methodName = value;
}
}
private string returnType;
public string ReturnType
{
get { return returnType; }
set
{
returnType = value;
}
}
}
好,现在我们只需要构造出代理类源码,然后动态编译出代理类的程序集,最后通过反射调用服务方法:
WebServiceProxyCreator
public class WebServiceProxyCreator
{
public Object WebServiceCaller(WebServiceParamaters parameters)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters codeParameters = new CompilerParameters();
codeParameters.GenerateExecutable = false;
codeParameters.GenerateInMemory = true;
StringBuilder code = new StringBuilder();
CreateProxyCode(code, parameters);
codeParameters.ReferencedAssemblies.Add("System.dll");
codeParameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
Type clientType = assembly.GetType("RuntimeServiceClient");
ConstructorInfo ci = clientType.GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
BasicHttpBinding binding = new BasicHttpBinding(); //只演示传统的WebService调用
EndpointAddress address = new EndpointAddress(parameters.address);
Object client = ci.Invoke(new object[] { binding, address });
MethodInfo mi = clientType.GetMethod(parameters.MethodName);
Object result = mi.Invoke(client, null);
mi = clientType.GetMethod("Close"); //关闭代理
mi.Invoke(client, null);
return result;
}
public static void CreateProxyCode(StringBuilder code, WebServiceParamaters parameters)
{
code.AppendLine("using System;");
code.AppendLine("using System.ServiceModel;");
code.AppendLine("using System.ServiceModel.Channels;");
code.Append(@"[ServiceContract(");
if (!String.IsNullOrEmpty(parameters.ServiceNamespace))
{
code.Append("Namespace=\"").Append(parameters.ServiceNamespace).Append("\"");
}
code.AppendLine(")]");
code.AppendLine("public interface IRuntimeService");
code.AppendLine("{ ");
code.Append("[OperationContract(");
if (!String.IsNullOrEmpty(parameters.MethodAction))
{
code.Append("Action=\"").Append(parameters.MethodAction).Append("\"");
if (!String.IsNullOrEmpty(parameters.MethodReplyAction))
{
code.Append(", ");
}
}
if (!String.IsNullOrEmpty(parameters.MethodReplyAction))
{
code.Append("ReplyAction=\"").Append(parameters.MethodReplyAction).Append("\"");
}
code.AppendLine(")]");
code.Append(parameters.ReturnType).Append(" ");
code.Append(parameters.MethodName).AppendLine("();");
code.AppendLine("}");
code.AppendLine();
code.AppendLine("public class RuntimeServiceClient : ClientBase<IRuntimeService>, IRuntimeService");
code.AppendLine("{ ");
code.AppendLine("public RuntimeServiceClient(Binding binding, EndpointAddress address) :base(binding, address)");
code.AppendLine("{ ");
code.AppendLine("}");
code.Append("public ").Append(parameters.ReturnType).Append(" ");
code.Append(parameters.MethodName).AppendLine("()");
code.AppendLine("{ ");
code.Append("return base.Channel.").Append(parameters.MethodName).AppendLine("();");
code.AppendLine("}");
code.AppendLine("}");
}
}
注意,由于代理类使用了WCF框架,所以编译时我们需要添加System.ServiceModel的引用,当然System.dll肯定是必须的,这里要注意,System.ServiceModel.dll应该保存到应用程序目录,否则动态编译时会引发异常,很简单,在工程引用中添加System.ServiceModel的引用,然后在属性中将拷贝到本地属性设置为true。
到此,我们就可以直接通过传入的服务地址、服务方法名称以及相关的命名空间,即可调用服务(尽管我们只能调用无参服务,并且尽管我们也只能调用使用BasicHttpBinding绑定的服务,这些限制的原因是…我懒,好吧,相信只要经过一点改动即可去掉这些限制)。
可惜,我们的程序还很傻:每次调用服务都需要去生成代码、编译、创建代理实例最后再调用,嗯…那就缓存吧:
在WebServiceParameters类中重写GetHashCode方法:
然后在WebServiceProxyCreator中加入缓存机制:
public class WebServiceProxyCreator
{
private static Dictionary<int, Type> proxyTypeCatch = new Dictionary<int, Type>();
public Object WebServiceCaller(WebServiceParamaters parameters)
{
int key = parameters.GetHashCode();
Type clientType = null;
if (proxyTypeCatch.ContainsKey(key))
{
clientType = proxyTypeCatch[key];
Debug.WriteLine("使用缓存");
}
else
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters codeParameters = new CompilerParameters();
codeParameters.GenerateExecutable = false;
codeParameters.GenerateInMemory = true;
StringBuilder code = new StringBuilder();
CreateProxyCode(code, parameters);
codeParameters.ReferencedAssemblies.Add("System.dll");
codeParameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
clientType = assembly.GetType("RuntimeServiceClient");
proxyTypeCatch.Add(key, clientType);
}
ConstructorInfo ci = clientType.GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
BasicHttpBinding binding = new BasicHttpBinding(); //只演示传统的WebService调用
EndpointAddress address = new EndpointAddress(parameters.address);
Object client = ci.Invoke(new object[] { binding, address });
MethodInfo mi = clientType.GetMethod(parameters.MethodName);
Object result = mi.Invoke(client, null);
mi = clientType.GetMethod("Close"); //关闭代理
mi.Invoke(client, null);
return result;
}
}