link这篇文章,本来是我写在csdn上的
http://blog.csdn.net/yangang0201/archive/2007/08/23/1756320.aspx
项目:xx 之 手工派单
 Code
Code
 [ClassInterface(ClassInterfaceType.AutoDual)]
[ClassInterface(ClassInterfaceType.AutoDual)]
 public class DynWebService : IDisposable
public class DynWebService : IDisposable


 {
{
 // Fields
    // Fields
 private Assembly assembly = null;
    private Assembly assembly = null;
 private const string defNameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
    private const string defNameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
 private string nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
    private string nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";

 // Methods
    // Methods
 public bool Binding(string url, string keyFile, ClassInterfaceType intfType)
    public bool Binding(string url, string keyFile, ClassInterfaceType intfType)

 
     {
{
 if (url.ToLower().EndsWith(".dll"))
        if (url.ToLower().EndsWith(".dll"))

 
         {
{
 this.assembly = Assembly.LoadFile(url);
            this.assembly = Assembly.LoadFile(url);
 return true;
            return true;
 }
        }
 string wsdlUrl = url;
        string wsdlUrl = url;
 CompilerResults results = this.CompilerCSharp(wsdlUrl, keyFile, intfType, false);
        CompilerResults results = this.CompilerCSharp(wsdlUrl, keyFile, intfType, false);
 if (results == null)
        if (results == null)

 
         {
{
 return false;
            return false;
 }
        }
 if (results.Errors.HasErrors)
        if (results.Errors.HasErrors)

 
         {
{
 StringBuilder builder = new StringBuilder();
            StringBuilder builder = new StringBuilder();
 foreach (CompilerError error in results.Errors)
            foreach (CompilerError error in results.Errors)

 
             {
{
 builder.Append(error.ToString());
                builder.Append(error.ToString());
 builder.Append(Environment.NewLine);
                builder.Append(Environment.NewLine);
 }
            }
 throw new Exception(builder.ToString());
            throw new Exception(builder.ToString());
 }
        }
 this.assembly = results.CompiledAssembly;
        this.assembly = results.CompiledAssembly;
 return true;
        return true;
 }
    }

 public DynWSClassBinding BindingClass(string className)
    public DynWSClassBinding BindingClass(string className)

 
     {
{
 if (this.assembly == null)
        if (this.assembly == null)

 
         {
{
 throw new Exception("请先绑定Web服务。");
            throw new Exception("请先绑定Web服务。");
 }
        }
 DynWSClassBinding binding = new DynWSClassBinding();
        DynWSClassBinding binding = new DynWSClassBinding();
 string str = this.FormatClassName(className);
        string str = this.FormatClassName(className);
 if (binding.BindingClass(this.assembly, str))
        if (binding.BindingClass(this.assembly, str))

 
         {
{
 return binding;
            return binding;
 }
        }
 return null;
        return null;
 }
    }

 public DynWSMethodBinding BindingMethod(string className, string methodName)
    public DynWSMethodBinding BindingMethod(string className, string methodName)

 
     {
{
 if (this.assembly == null)
        if (this.assembly == null)

 
         {
{
 throw new Exception("请先绑定Web服务。");
            throw new Exception("请先绑定Web服务。");
 }
        }
 string str = "";
        string str = "";
 if ((className == null) || (className.Length == 0))
        if ((className == null) || (className.Length == 0))

 
         {
{
 str = this.FindClassNameByMethoadName(methodName);
            str = this.FindClassNameByMethoadName(methodName);
 }
        }
 else
        else

 
         {
{
 str = this.FormatClassName(className);
            str = this.FormatClassName(className);
 }
        }
 DynWSClassBinding binding = this.BindingClass(str);
        DynWSClassBinding binding = this.BindingClass(str);
 if (binding == null)
        if (binding == null)

 
         {
{
 return null;
            return null;
 }
        }
 return binding.BindingMethod(methodName);
        return binding.BindingMethod(methodName);
 }
    }

 public CompilerResults CompilerCSharp(string wsdlUrl, string keyFile, ClassInterfaceType intfType, bool generateInMemory)
    public CompilerResults CompilerCSharp(string wsdlUrl, string keyFile, ClassInterfaceType intfType, bool generateInMemory)

 
     {
{
 CodeCompileUnit compilationUnit = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType);
        CodeCompileUnit compilationUnit = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType);
 if (compilationUnit == null)
        if (compilationUnit == null)

 
         {
{
 return null;
            return null;
 }
        }
 ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
        ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
 CompilerParameters options = new CompilerParameters();
        CompilerParameters options = new CompilerParameters();
 options.GenerateExecutable = false;
        options.GenerateExecutable = false;
 options.GenerateInMemory = generateInMemory;
        options.GenerateInMemory = generateInMemory;
 options.ReferencedAssemblies.Add("System.dll");
        options.ReferencedAssemblies.Add("System.dll");
 options.ReferencedAssemblies.Add("System.XML.dll");
        options.ReferencedAssemblies.Add("System.XML.dll");
 options.ReferencedAssemblies.Add("System.Web.Services.dll");
        options.ReferencedAssemblies.Add("System.Web.Services.dll");
 options.ReferencedAssemblies.Add("System.Data.dll");
        options.ReferencedAssemblies.Add("System.Data.dll");
 return compiler.CompileAssemblyFromDom(options, compilationUnit);
        return compiler.CompileAssemblyFromDom(options, compilationUnit);
 }
    }

 public void Dispose()
    public void Dispose()

 
     {
{
 Assembly assembly = this.assembly;
        Assembly assembly = this.assembly;
 }
    }

 public string FindClassNameByMethoadName(string methodName)
    public string FindClassNameByMethoadName(string methodName)

 
     {
{
 foreach (Type type in this.assembly.GetExportedTypes())
        foreach (Type type in this.assembly.GetExportedTypes())

 
         {
{
 if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))
            if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))

 
             {
{
 MethodInfo method = null;
                MethodInfo method = null;
 try
                try

 
                 {
{
 method = type.GetMethod(methodName);
                    method = type.GetMethod(methodName);
 }
                }
 catch (Exception exception)
                catch (Exception exception)

 
                 {
{
 throw new Exception(string.Format("从{0}查找方法{1}异常:{2}", type.FullName, methodName, exception.Message));
                    throw new Exception(string.Format("从{0}查找方法{1}异常:{2}", type.FullName, methodName, exception.Message));
 }
                }
 if (method != null)
                if (method != null)

 
                 {
{
 return type.FullName;
                    return type.FullName;
 }
                }
 }
            }
 }
        }
 return "";
        return "";
 }
    }

 private string FormatClassName(string cls)
    private string FormatClassName(string cls)

 
     {
{
 if (cls.Length == 0)
        if (cls.Length == 0)

 
         {
{
 return this.GetFirstClassName();
            return this.GetFirstClassName();
 }
        }
 if ((this.nameSpace.Length > 0) && !cls.StartsWith(this.nameSpace))
        if ((this.nameSpace.Length > 0) && !cls.StartsWith(this.nameSpace))

 
         {
{
 cls = this.nameSpace + "." + cls;
            cls = this.nameSpace + "." + cls;
 }
        }
 return cls;
        return cls;
 }
    }

 private string FormatWSDLUrl(string url)
    private string FormatWSDLUrl(string url)

 
     {
{
 string str = url.ToLower();
        string str = url.ToLower();
 if (!str.StartsWith("http:"))
        if (!str.StartsWith("http:"))

 
         {
{
 return url;
            return url;
 }
        }
 if (str.EndsWith("?wsdl"))
        if (str.EndsWith("?wsdl"))

 
         {
{
 return url;
            return url;
 }
        }
 return (url + "?WSDL");
        return (url + "?WSDL");
 }
    }

 private CodeCompileUnit GeneratorCodeUnit(string wsdlUrl, string keyFile, ClassInterfaceType intfType)
    private CodeCompileUnit GeneratorCodeUnit(string wsdlUrl, string keyFile, ClassInterfaceType intfType)

 
     {
{
 if (this.nameSpace.Length == 0)
        if (this.nameSpace.Length == 0)

 
         {
{
 this.nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
            this.nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
 }
        }
 WebClient client = new WebClient();
        WebClient client = new WebClient();
 ServiceDescription description = ServiceDescription.Read(client.OpenRead(this.FormatWSDLUrl(wsdlUrl)));
        ServiceDescription description = ServiceDescription.Read(client.OpenRead(this.FormatWSDLUrl(wsdlUrl)));
 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
        ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
 importer.AddServiceDescription(description, "", "");
        importer.AddServiceDescription(description, "", "");
 CodeCompileUnit unit = new CodeCompileUnit();
        CodeCompileUnit unit = new CodeCompileUnit();
 if ((keyFile != null) && (keyFile.Length > 0))
        if ((keyFile != null) && (keyFile.Length > 0))

 
         {
{

 unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyFile", new CodeAttributeArgument[]
            unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyFile", new CodeAttributeArgument[] 
 { new CodeAttributeArgument(new CodePrimitiveExpression(keyFile)) }));
{ new CodeAttributeArgument(new CodePrimitiveExpression(keyFile)) }));

 unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyName", new CodeAttributeArgument[]
            unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyName", new CodeAttributeArgument[] 
 { new CodeAttributeArgument(new CodePrimitiveExpression("")) }));
{ new CodeAttributeArgument(new CodePrimitiveExpression("")) }));
 }
        }
 CodeNamespace namespace2 = new CodeNamespace(this.nameSpace);
        CodeNamespace namespace2 = new CodeNamespace(this.nameSpace);
 unit.Namespaces.Add(namespace2);
        unit.Namespaces.Add(namespace2);
 importer.Import(namespace2, unit);
        importer.Import(namespace2, unit);
 if (intfType != ClassInterfaceType.None)
        if (intfType != ClassInterfaceType.None)

 
         {
{
 foreach (CodeNamespace namespace3 in unit.Namespaces)
            foreach (CodeNamespace namespace3 in unit.Namespaces)

 
             {
{
 foreach (CodeTypeDeclaration declaration in namespace3.Types)
                foreach (CodeTypeDeclaration declaration in namespace3.Types)

 
                 {
{

 declaration.CustomAttributes.Insert(0, new CodeAttributeDeclaration("System.Runtime.InteropServices.ClassInterfaceAttribute", new CodeAttributeArgument[]
                    declaration.CustomAttributes.Insert(0, new CodeAttributeDeclaration("System.Runtime.InteropServices.ClassInterfaceAttribute", new CodeAttributeArgument[] 
 { new CodeAttributeArgument(new CodePrimitiveExpression((int) intfType)) }));
{ new CodeAttributeArgument(new CodePrimitiveExpression((int) intfType)) }));
 }
                }
 }
            }
 }
        }
 return unit;
        return unit;
 }
    }

 public bool GeneratorCSharpCode(string wsdlUrl, string keyFile, ClassInterfaceType intfType, out string code)
    public bool GeneratorCSharpCode(string wsdlUrl, string keyFile, ClassInterfaceType intfType, out string code)

 
     {
{
 code = null;
       code = null;
 CodeCompileUnit e = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType);
        CodeCompileUnit e = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType);
 if (e == null)
        if (e == null)

 
         {
{
 return false;
            return false;
 }
        }
 new CSharpCodeProvider();
        new CSharpCodeProvider();
 ICodeGenerator generator = new CSharpCodeProvider().CreateGenerator();
        ICodeGenerator generator = new CSharpCodeProvider().CreateGenerator();
 CodeGeneratorOptions o = new CodeGeneratorOptions();
        CodeGeneratorOptions o = new CodeGeneratorOptions();
 o.BlankLinesBetweenMembers = true;
        o.BlankLinesBetweenMembers = true;
 o.BracingStyle = "C";
        o.BracingStyle = "C";
 o.ElseOnClosing = false;
        o.ElseOnClosing = false;
 o.IndentString = "    ";
        o.IndentString = "    ";
 StringBuilder sb = new StringBuilder();
        StringBuilder sb = new StringBuilder();
 StringWriter w = new StringWriter(sb);
        StringWriter w = new StringWriter(sb);
 generator.GenerateCodeFromCompileUnit(e, w, o);
        generator.GenerateCodeFromCompileUnit(e, w, o);
 code = sb.ToString();
        code = sb.ToString();
 w.Close();
        w.Close();
 return true;
        return true;
 }
    }

 public string GetFirstClassName()
    public string GetFirstClassName()

 
     {
{
 foreach (Type type in this.assembly.GetExportedTypes())
        foreach (Type type in this.assembly.GetExportedTypes())

 
         {
{
 if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))
            if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))

 
             {
{
 return type.FullName;
                return type.FullName;
 }
            }
 }
        }
 return "";
        return "";
 }
    }

 // Properties
    // Properties
 public string NameSpace
    public string NameSpace

 
     {
{
 get
        get

 
         {
{
 return this.nameSpace;
            return this.nameSpace;
 }
        }
 set
        set

 
         {
{
 if ((value != null) && (value.Length > 0))
            if ((value != null) && (value.Length > 0))

 
             {
{
 this.nameSpace = value;
                this.nameSpace = value;
 }
            }
 else
            else

 
             {
{
 this.nameSpace = "";
                this.nameSpace = "";
 }
            }
 }
        }
 }
    }
 }
}

 
 
 [ClassInterface(ClassInterfaceType.AutoDual)]
[ClassInterface(ClassInterfaceType.AutoDual)]
 public class DynWSClassBinding : IDisposable
public class DynWSClassBinding : IDisposable


 {
{
 // Fields
    // Fields
 private Assembly assembly = null;
    private Assembly assembly = null;
 private string className = "";
    private string className = "";
 private object classObject = null;
    private object classObject = null;
 private SoapHttpClientProtocol classObjectSoapHttpClient = null;
    private SoapHttpClientProtocol classObjectSoapHttpClient = null;
 private Type classType = null;
    private Type classType = null;

 // Methods
    // Methods
 public bool BindingClass(Assembly assembly, string className)
    public bool BindingClass(Assembly assembly, string className)

 
     {
{
 this.assembly = assembly;
        this.assembly = assembly;
 this.className = className;
        this.className = className;
 this.classType = this.assembly.GetType(this.className, true, true);
        this.classType = this.assembly.GetType(this.className, true, true);
 this.classObject = Activator.CreateInstance(this.classType);
        this.classObject = Activator.CreateInstance(this.classType);
 try
        try

 
         {
{
 this.classObjectSoapHttpClient = this.classObject as SoapHttpClientProtocol;
            this.classObjectSoapHttpClient = this.classObject as SoapHttpClientProtocol;
 }
        }
 catch
        catch

 
         {
{
 }
        }
 return true;
        return true;
 }
    }

 public DynWSMethodBinding BindingMethod(string methodName)
    public DynWSMethodBinding BindingMethod(string methodName)

 
     {
{
 if ((this.assembly == null) || (this.classObject == null))
        if ((this.assembly == null) || (this.classObject == null))

 
         {
{
 throw new Exception("请先绑定类。");
            throw new Exception("请先绑定类。");
 }
        }
 DynWSMethodBinding binding = new DynWSMethodBinding();
        DynWSMethodBinding binding = new DynWSMethodBinding();
 if (binding.BindingMethod(this, methodName))
        if (binding.BindingMethod(this, methodName))

 
         {
{
 return binding;
            return binding;
 }
        }
 return null;
        return null;
 }
    }

 public void Dispose()
    public void Dispose()

 
     {
{
 if (this.classObjectSoapHttpClient != null)
        if (this.classObjectSoapHttpClient != null)

 
         {
{
 this.classObjectSoapHttpClient.Dispose();
            this.classObjectSoapHttpClient.Dispose();
 }
        }
 }
    }

 public object GetFieldValue(string attName)
    public object GetFieldValue(string attName)

 
     {
{
 if (this.classObject == null)
        if (this.classObject == null)

 
         {
{
 throw new Exception("请先绑定类。");
            throw new Exception("请先绑定类。");
 }
        }
 try
        try

 
         {
{
 return this.classType.InvokeMember(attName, BindingFlags.GetProperty, null, this.classObject, new object[0]);
            return this.classType.InvokeMember(attName, BindingFlags.GetProperty, null, this.classObject, new object[0]);
 }
        }
 catch
        catch

 
         {
{
 return this.classType.InvokeMember(attName, BindingFlags.GetField, null, this.classObject, new object[0]);
            return this.classType.InvokeMember(attName, BindingFlags.GetField, null, this.classObject, new object[0]);
 }
        }
 }
    }

 public bool SetFieldValue(string attName, object attValue)
    public bool SetFieldValue(string attName, object attValue)

 
     {
{
 if (this.classObject == null)
        if (this.classObject == null)

 
         {
{
 throw new Exception("请先绑定类。");
            throw new Exception("请先绑定类。");
 }
        }
 try
        try

 
         {
{

 this.classType.InvokeMember(attName, BindingFlags.SetProperty, null, this.classObject, new object[]
            this.classType.InvokeMember(attName, BindingFlags.SetProperty, null, this.classObject, new object[] 
 { attValue });
{ attValue });
 }
        }
 catch
        catch

 
         {
{

 this.classType.InvokeMember(attName, BindingFlags.SetField, null, this.classObject, new object[]
            this.classType.InvokeMember(attName, BindingFlags.SetField, null, this.classObject, new object[] 
 { attValue });
{ attValue });
 }
        }
 return true;
        return true;
 }
    }

 // Properties
    // Properties
 public Assembly Assembly
    public Assembly Assembly

 
     {
{
 get
        get

 
         {
{
 return this.assembly;
            return this.assembly;
 }
        }
 }
    }

 public string ClassName
    public string ClassName

 
     {
{
 get
        get

 
         {
{
 return this.className;
            return this.className;
 }
        }
 }
    }

 public object ClassObject
    public object ClassObject

 
     {
{
 get
        get

 
         {
{
 return this.classObject;
            return this.classObject;
 }
        }
 }
    }

 public Type ClassType
    public Type ClassType

 
     {
{
 get
        get

 
         {
{
 return this.classType;
            return this.classType;
 }
        }
 }
    }

 public string Url
    public string Url

 
     {
{
 get
        get

 
         {
{
 return this.GetFieldValue("Url").ToString();
            return this.GetFieldValue("Url").ToString();
 }
        }
 set
        set

 
         {
{
 this.SetFieldValue("Url", value);
            this.SetFieldValue("Url", value);
 }
        }
 }
    }
 }
}

 
 
 [ClassInterface(ClassInterfaceType.AutoDual)]
 [ClassInterface(ClassInterfaceType.AutoDual)]
 public class DynWSMethodBinding : IDisposable
public class DynWSMethodBinding : IDisposable


 {
{
 // Fields
    // Fields
 private DynWSClassBinding classBinding = null;
    private DynWSClassBinding classBinding = null;
 private MethodInfo methodInfo = null;
    private MethodInfo methodInfo = null;
 private string methodName = "";
    private string methodName = "";
 private ParameterInfo[] methodParams = null;
    private ParameterInfo[] methodParams = null;
 private object[] methodParamsValue = null;
    private object[] methodParamsValue = null;

 // Methods
    // Methods
 public bool BindingMethod(DynWSClassBinding classBND, string methodName)
    public bool BindingMethod(DynWSClassBinding classBND, string methodName)

 
     {
{
 this.classBinding = classBND;
        this.classBinding = classBND;
 this.methodName = methodName;
        this.methodName = methodName;
 this.methodInfo = this.classBinding.ClassType.GetMethod(this.methodName);
        this.methodInfo = this.classBinding.ClassType.GetMethod(this.methodName);
 if (this.methodInfo != null)
        if (this.methodInfo != null)

 
         {
{
 this.methodParams = this.methodInfo.GetParameters();
            this.methodParams = this.methodInfo.GetParameters();
 this.methodParamsValue = new object[this.methodParams.Length];
            this.methodParamsValue = new object[this.methodParams.Length];
 return true;
            return true;
 }
        }
 return false;
        return false;
 }
    }

 public void ClearParamsValue()
    public void ClearParamsValue()

 
     {
{
 this.methodParamsValue = new object[this.methodParams.Length];
        this.methodParamsValue = new object[this.methodParams.Length];
 }
    }

 public void Dispose()
    public void Dispose()

 
     {
{
 if (this.classBinding != null)
        if (this.classBinding != null)

 
         {
{
 this.classBinding.Dispose();
            this.classBinding.Dispose();
 }
        }
 }
    }

 private int GetParamIndex(string paramName)
    private int GetParamIndex(string paramName)

 
     {
{
 if (this.methodParams != null)
        if (this.methodParams != null)

 
         {
{
 foreach (ParameterInfo info in this.methodParams)
            foreach (ParameterInfo info in this.methodParams)

 
             {
{
 if (info.Name.ToLower() == paramName.ToLower())
                if (info.Name.ToLower() == paramName.ToLower())

 
                
 {
{
 return info.Position;
                    return info.Position;
 }
                }
 }
            }
 }
        }
 return -1;
        return -1;
 }
    }

 public ParameterInfo GetParamInfo(string paramName)
    public ParameterInfo GetParamInfo(string paramName)

 
     {
{
 foreach (ParameterInfo info in this.methodParams)
        foreach (ParameterInfo info in this.methodParams)

 
         {
{
 if (info.Name.ToLower() == paramName.ToLower())
            if (info.Name.ToLower() == paramName.ToLower())

 
             {
{
 return info;
                return info;
 }
            }
 }
        }
 return null;
        return null;
 }
    }

 public string GetParamInfoString()
    public string GetParamInfoString()

 
     {
{
 StringBuilder builder = new StringBuilder();
        StringBuilder builder = new StringBuilder();
 if (this.methodParams != null)
        if (this.methodParams != null)

 
         {
{
 int num = 0;
            int num = 0;
 foreach (ParameterInfo info in this.methodParams)
            foreach (ParameterInfo info in this.methodParams)

 
             {
{
 object obj2 = (this.methodParamsValue != null) ? this.methodParamsValue[info.Position] : null;
                object obj2 = (this.methodParamsValue != null) ? this.methodParamsValue[info.Position] : null;
 string str = (obj2 == null) ? "<null>" : obj2.ToString();
                string str = (obj2 == null) ? "<null>" : obj2.ToString();

 builder.Append(string.Format("序号:{0}/{1} 参数:{2} 值:{3} 类型:{4} ", new object[]
                builder.Append(string.Format("序号:{0}/{1} 参数:{2} 值:{3} 类型:{4} ", new object[]  { num++, info.Position, info.Name, str, info.ParameterType }));
{ num++, info.Position, info.Name, str, info.ParameterType }));
 }
            }
 }
        }
 return builder.ToString();
        return builder.ToString();
 }
    }

 public object Invoke()
    public object Invoke()

 
     {
{
 return this.methodInfo.Invoke(this.classBinding.ClassObject, this.methodParamsValue);
        return this.methodInfo.Invoke(this.classBinding.ClassObject, this.methodParamsValue);
 }
    }

 public bool SetParamValue(string paramName, object paramValue)
    public bool SetParamValue(string paramName, object paramValue)

 
     {
{
 try
        try

 
         {
{
 object obj2 = null;
            object obj2 = null;
 foreach (ParameterInfo info in this.methodParams)
            foreach (ParameterInfo info in this.methodParams)

 
             {
{
 if (info.Name.ToLower() == paramName.ToLower())
                if (info.Name.ToLower() == paramName.ToLower())

 
                 {
{
 obj2 = Convert.ChangeType(paramValue, info.ParameterType);
                    obj2 = Convert.ChangeType(paramValue, info.ParameterType);
 this.methodParamsValue[info.Position] = obj2;
                    this.methodParamsValue[info.Position] = obj2;
 return true;
                    return true;
 }
                }
 }
            }
 return false;
            return false;
 }
        }
 catch
        catch

 
         {
{
 return false;
            return false;
 }
        }
 }
    }

 // Properties
    // Properties
 public DynWSClassBinding ClassBinding
    public DynWSClassBinding ClassBinding

 
    {
{
 get
        get

 
         {
{
 return this.classBinding;
            return this.classBinding;
 }
        }
 }
    }

 public MethodInfo MethodInfo
    public MethodInfo MethodInfo

 
     {
{
 get
        get

 
         {
{
 return this.methodInfo;
            return this.methodInfo;
 }
        }
 }
    }

 public string MethodName
    public string MethodName

 
     {
{
 get
        get

 
         {
{
 return this.methodName;
            return this.methodName;
 }
        }
 }
    }

 public ParameterInfo[] Params
    public ParameterInfo[] Params

 
     {
{
 get
        get

 
         {
{
 return this.methodParams;
            return this.methodParams;
 }
        }
 }
    }

 public object[] ParamValues
    public object[] ParamValues

 
     {
{
 get
        get

 
         {
{
 return this.methodParamsValue;
            return this.methodParamsValue;
 }
        }
 }
    }
 }
}

 
 

 Code
Code
 使用介绍:
使用介绍:

 
 

 public bool CheckUser(string strUName, string strPwd)
 public bool CheckUser(string strUName, string strPwd)

 
         {
{
 try
            try

 
             {
{
 DynWebService ws = new DynWebService();
                DynWebService ws = new DynWebService();
 ws.Binding(WSDLUrl, "", System.Runtime.InteropServices.ClassInterfaceType.None);
                ws.Binding(WSDLUrl, "", System.Runtime.InteropServices.ClassInterfaceType.None);
 DynWSClassBinding cls = ws.BindingClass("");
                DynWSClassBinding cls = ws.BindingClass("");
 cls.Url = Url;
                cls.Url = Url;
 DynWSMethodBinding method = cls.BindingMethod("eomsAuthentication");
                DynWSMethodBinding method = cls.BindingMethod("eomsAuthentication");
 method.SetParamValue("serSupplier", "");
                method.SetParamValue("serSupplier", "");
 method.SetParamValue("serCaller", "");
                method.SetParamValue("serCaller", "");
 method.SetParamValue("callerPwd", "");
                method.SetParamValue("callerPwd", "");
 //pMethod->SetParamValue(_bstr_t("serSupplier"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Supplier")));
                //pMethod->SetParamValue(_bstr_t("serSupplier"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Supplier")));
 //pMethod->SetParamValue(_bstr_t("serCaller"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Caller")));
                //pMethod->SetParamValue(_bstr_t("serCaller"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Caller")));
 //CString strCallerPwd = (LPCTSTR)CGlobal::GetAppSetting("CallerPwd");
                //CString strCallerPwd = (LPCTSTR)CGlobal::GetAppSetting("CallerPwd");
 //strCallerPwd = CEnc::DecodeAndAutoCheck(strCallerPwd);
                //strCallerPwd = CEnc::DecodeAndAutoCheck(strCallerPwd);
 string callTime = DateTime.Now.ToString("yyyy-mm-hh HH:MM:SS");
                string callTime = DateTime.Now.ToString("yyyy-mm-hh HH:MM:SS");
 method.SetParamValue("callTime", callTime);
                method.SetParamValue("callTime", callTime);
 method.SetParamValue("userName", strUName);
                method.SetParamValue("userName", strUName);
 method.SetParamValue("userPassword", strPwd);
                method.SetParamValue("userPassword", strPwd);

 object ret = method.Invoke();
                object ret = method.Invoke();
 string sret = ret == null ? "" : ret.ToString();
                string sret = ret == null ? "" : ret.ToString();
 if (sret.Length == 0)
                if (sret.Length == 0)

 
                 {
{
 return true;
                    return true;
 }
                }
 else
                else

 
                 {
{
 throw new Exception("登录失败:" + sret);
                    throw new Exception("登录失败:" + sret);
 }
                }
 }
            }
 catch
            catch

 
             {
{
 return false;
                return false;
 }
            }
 }
        }
http://blog.csdn.net/yangang0201/archive/2007/08/23/1756320.aspx
项目:xx 之 手工派单
内容介绍:/Files/yanchanggang/BOCO.APP.Common.DynWSCall.DynWSCallLib.rar
在本系统中,考虑到各个大区emos系统提供的鉴权服务 webserverce的地址不同,因此,不可能在系统中 把wsdl路径设定死,需要可以动态的引用wsdl.幸好,公司同事给了个dll,满足了该功能.
dll 文件:

 Code
Code [ClassInterface(ClassInterfaceType.AutoDual)]
[ClassInterface(ClassInterfaceType.AutoDual)] public class DynWebService : IDisposable
public class DynWebService : IDisposable

 {
{ // Fields
    // Fields private Assembly assembly = null;
    private Assembly assembly = null; private const string defNameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
    private const string defNameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace"; private string nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
    private string nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
 // Methods
    // Methods public bool Binding(string url, string keyFile, ClassInterfaceType intfType)
    public bool Binding(string url, string keyFile, ClassInterfaceType intfType)
 
     {
{ if (url.ToLower().EndsWith(".dll"))
        if (url.ToLower().EndsWith(".dll"))
 
         {
{ this.assembly = Assembly.LoadFile(url);
            this.assembly = Assembly.LoadFile(url); return true;
            return true; }
        } string wsdlUrl = url;
        string wsdlUrl = url; CompilerResults results = this.CompilerCSharp(wsdlUrl, keyFile, intfType, false);
        CompilerResults results = this.CompilerCSharp(wsdlUrl, keyFile, intfType, false); if (results == null)
        if (results == null)
 
         {
{ return false;
            return false; }
        } if (results.Errors.HasErrors)
        if (results.Errors.HasErrors)
 
         {
{ StringBuilder builder = new StringBuilder();
            StringBuilder builder = new StringBuilder(); foreach (CompilerError error in results.Errors)
            foreach (CompilerError error in results.Errors)
 
             {
{ builder.Append(error.ToString());
                builder.Append(error.ToString()); builder.Append(Environment.NewLine);
                builder.Append(Environment.NewLine); }
            } throw new Exception(builder.ToString());
            throw new Exception(builder.ToString()); }
        } this.assembly = results.CompiledAssembly;
        this.assembly = results.CompiledAssembly; return true;
        return true; }
    }
 public DynWSClassBinding BindingClass(string className)
    public DynWSClassBinding BindingClass(string className)
 
     {
{ if (this.assembly == null)
        if (this.assembly == null)
 
         {
{ throw new Exception("请先绑定Web服务。");
            throw new Exception("请先绑定Web服务。"); }
        } DynWSClassBinding binding = new DynWSClassBinding();
        DynWSClassBinding binding = new DynWSClassBinding(); string str = this.FormatClassName(className);
        string str = this.FormatClassName(className); if (binding.BindingClass(this.assembly, str))
        if (binding.BindingClass(this.assembly, str))
 
         {
{ return binding;
            return binding; }
        } return null;
        return null; }
    }
 public DynWSMethodBinding BindingMethod(string className, string methodName)
    public DynWSMethodBinding BindingMethod(string className, string methodName)
 
     {
{ if (this.assembly == null)
        if (this.assembly == null)
 
         {
{ throw new Exception("请先绑定Web服务。");
            throw new Exception("请先绑定Web服务。"); }
        } string str = "";
        string str = ""; if ((className == null) || (className.Length == 0))
        if ((className == null) || (className.Length == 0))
 
         {
{ str = this.FindClassNameByMethoadName(methodName);
            str = this.FindClassNameByMethoadName(methodName); }
        } else
        else
 
         {
{ str = this.FormatClassName(className);
            str = this.FormatClassName(className); }
        } DynWSClassBinding binding = this.BindingClass(str);
        DynWSClassBinding binding = this.BindingClass(str); if (binding == null)
        if (binding == null)
 
         {
{ return null;
            return null; }
        } return binding.BindingMethod(methodName);
        return binding.BindingMethod(methodName); }
    }
 public CompilerResults CompilerCSharp(string wsdlUrl, string keyFile, ClassInterfaceType intfType, bool generateInMemory)
    public CompilerResults CompilerCSharp(string wsdlUrl, string keyFile, ClassInterfaceType intfType, bool generateInMemory)
 
     {
{ CodeCompileUnit compilationUnit = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType);
        CodeCompileUnit compilationUnit = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType); if (compilationUnit == null)
        if (compilationUnit == null)
 
         {
{ return null;
            return null; }
        } ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
        ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler(); CompilerParameters options = new CompilerParameters();
        CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false;
        options.GenerateExecutable = false; options.GenerateInMemory = generateInMemory;
        options.GenerateInMemory = generateInMemory; options.ReferencedAssemblies.Add("System.dll");
        options.ReferencedAssemblies.Add("System.dll"); options.ReferencedAssemblies.Add("System.XML.dll");
        options.ReferencedAssemblies.Add("System.XML.dll"); options.ReferencedAssemblies.Add("System.Web.Services.dll");
        options.ReferencedAssemblies.Add("System.Web.Services.dll"); options.ReferencedAssemblies.Add("System.Data.dll");
        options.ReferencedAssemblies.Add("System.Data.dll"); return compiler.CompileAssemblyFromDom(options, compilationUnit);
        return compiler.CompileAssemblyFromDom(options, compilationUnit); }
    }
 public void Dispose()
    public void Dispose()
 
     {
{ Assembly assembly = this.assembly;
        Assembly assembly = this.assembly; }
    }
 public string FindClassNameByMethoadName(string methodName)
    public string FindClassNameByMethoadName(string methodName)
 
     {
{ foreach (Type type in this.assembly.GetExportedTypes())
        foreach (Type type in this.assembly.GetExportedTypes())
 
         {
{ if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))
            if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))
 
             {
{ MethodInfo method = null;
                MethodInfo method = null; try
                try
 
                 {
{ method = type.GetMethod(methodName);
                    method = type.GetMethod(methodName); }
                } catch (Exception exception)
                catch (Exception exception)
 
                 {
{ throw new Exception(string.Format("从{0}查找方法{1}异常:{2}", type.FullName, methodName, exception.Message));
                    throw new Exception(string.Format("从{0}查找方法{1}异常:{2}", type.FullName, methodName, exception.Message)); }
                } if (method != null)
                if (method != null)
 
                 {
{ return type.FullName;
                    return type.FullName; }
                } }
            } }
        } return "";
        return ""; }
    }
 private string FormatClassName(string cls)
    private string FormatClassName(string cls)
 
     {
{ if (cls.Length == 0)
        if (cls.Length == 0)
 
         {
{ return this.GetFirstClassName();
            return this.GetFirstClassName(); }
        } if ((this.nameSpace.Length > 0) && !cls.StartsWith(this.nameSpace))
        if ((this.nameSpace.Length > 0) && !cls.StartsWith(this.nameSpace))
 
         {
{ cls = this.nameSpace + "." + cls;
            cls = this.nameSpace + "." + cls; }
        } return cls;
        return cls; }
    }
 private string FormatWSDLUrl(string url)
    private string FormatWSDLUrl(string url)
 
     {
{ string str = url.ToLower();
        string str = url.ToLower(); if (!str.StartsWith("http:"))
        if (!str.StartsWith("http:"))
 
         {
{ return url;
            return url; }
        } if (str.EndsWith("?wsdl"))
        if (str.EndsWith("?wsdl"))
 
         {
{ return url;
            return url; }
        } return (url + "?WSDL");
        return (url + "?WSDL"); }
    }
 private CodeCompileUnit GeneratorCodeUnit(string wsdlUrl, string keyFile, ClassInterfaceType intfType)
    private CodeCompileUnit GeneratorCodeUnit(string wsdlUrl, string keyFile, ClassInterfaceType intfType)
 
     {
{ if (this.nameSpace.Length == 0)
        if (this.nameSpace.Length == 0)
 
         {
{ this.nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace";
            this.nameSpace = "BOCO.APP.Common.DynWSCallDefaultNameSpace"; }
        } WebClient client = new WebClient();
        WebClient client = new WebClient(); ServiceDescription description = ServiceDescription.Read(client.OpenRead(this.FormatWSDLUrl(wsdlUrl)));
        ServiceDescription description = ServiceDescription.Read(client.OpenRead(this.FormatWSDLUrl(wsdlUrl))); ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
        ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); importer.AddServiceDescription(description, "", "");
        importer.AddServiceDescription(description, "", ""); CodeCompileUnit unit = new CodeCompileUnit();
        CodeCompileUnit unit = new CodeCompileUnit(); if ((keyFile != null) && (keyFile.Length > 0))
        if ((keyFile != null) && (keyFile.Length > 0))
 
         {
{
 unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyFile", new CodeAttributeArgument[]
            unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyFile", new CodeAttributeArgument[] 
 { new CodeAttributeArgument(new CodePrimitiveExpression(keyFile)) }));
{ new CodeAttributeArgument(new CodePrimitiveExpression(keyFile)) }));
 unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyName", new CodeAttributeArgument[]
            unit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyName", new CodeAttributeArgument[] 
 { new CodeAttributeArgument(new CodePrimitiveExpression("")) }));
{ new CodeAttributeArgument(new CodePrimitiveExpression("")) })); }
        } CodeNamespace namespace2 = new CodeNamespace(this.nameSpace);
        CodeNamespace namespace2 = new CodeNamespace(this.nameSpace); unit.Namespaces.Add(namespace2);
        unit.Namespaces.Add(namespace2); importer.Import(namespace2, unit);
        importer.Import(namespace2, unit); if (intfType != ClassInterfaceType.None)
        if (intfType != ClassInterfaceType.None)
 
         {
{ foreach (CodeNamespace namespace3 in unit.Namespaces)
            foreach (CodeNamespace namespace3 in unit.Namespaces)
 
             {
{ foreach (CodeTypeDeclaration declaration in namespace3.Types)
                foreach (CodeTypeDeclaration declaration in namespace3.Types)
 
                 {
{
 declaration.CustomAttributes.Insert(0, new CodeAttributeDeclaration("System.Runtime.InteropServices.ClassInterfaceAttribute", new CodeAttributeArgument[]
                    declaration.CustomAttributes.Insert(0, new CodeAttributeDeclaration("System.Runtime.InteropServices.ClassInterfaceAttribute", new CodeAttributeArgument[] 
 { new CodeAttributeArgument(new CodePrimitiveExpression((int) intfType)) }));
{ new CodeAttributeArgument(new CodePrimitiveExpression((int) intfType)) })); }
                } }
            } }
        } return unit;
        return unit; }
    }
 public bool GeneratorCSharpCode(string wsdlUrl, string keyFile, ClassInterfaceType intfType, out string code)
    public bool GeneratorCSharpCode(string wsdlUrl, string keyFile, ClassInterfaceType intfType, out string code)
 
     {
{ code = null;
       code = null; CodeCompileUnit e = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType);
        CodeCompileUnit e = this.GeneratorCodeUnit(wsdlUrl, keyFile, intfType); if (e == null)
        if (e == null)
 
         {
{ return false;
            return false; }
        } new CSharpCodeProvider();
        new CSharpCodeProvider(); ICodeGenerator generator = new CSharpCodeProvider().CreateGenerator();
        ICodeGenerator generator = new CSharpCodeProvider().CreateGenerator(); CodeGeneratorOptions o = new CodeGeneratorOptions();
        CodeGeneratorOptions o = new CodeGeneratorOptions(); o.BlankLinesBetweenMembers = true;
        o.BlankLinesBetweenMembers = true; o.BracingStyle = "C";
        o.BracingStyle = "C"; o.ElseOnClosing = false;
        o.ElseOnClosing = false; o.IndentString = "    ";
        o.IndentString = "    "; StringBuilder sb = new StringBuilder();
        StringBuilder sb = new StringBuilder(); StringWriter w = new StringWriter(sb);
        StringWriter w = new StringWriter(sb); generator.GenerateCodeFromCompileUnit(e, w, o);
        generator.GenerateCodeFromCompileUnit(e, w, o); code = sb.ToString();
        code = sb.ToString(); w.Close();
        w.Close(); return true;
        return true; }
    }
 public string GetFirstClassName()
    public string GetFirstClassName()
 
     {
{ foreach (Type type in this.assembly.GetExportedTypes())
        foreach (Type type in this.assembly.GetExportedTypes())
 
         {
{ if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))
            if (type.IsSubclassOf(typeof(SoapHttpClientProtocol)))
 
             {
{ return type.FullName;
                return type.FullName; }
            } }
        } return "";
        return ""; }
    }
 // Properties
    // Properties public string NameSpace
    public string NameSpace
 
     {
{ get
        get
 
         {
{ return this.nameSpace;
            return this.nameSpace; }
        } set
        set
 
         {
{ if ((value != null) && (value.Length > 0))
            if ((value != null) && (value.Length > 0))
 
             {
{ this.nameSpace = value;
                this.nameSpace = value; }
            } else
            else
 
             {
{ this.nameSpace = "";
                this.nameSpace = ""; }
            } }
        } }
    } }
}
 
  [ClassInterface(ClassInterfaceType.AutoDual)]
[ClassInterface(ClassInterfaceType.AutoDual)] public class DynWSClassBinding : IDisposable
public class DynWSClassBinding : IDisposable

 {
{ // Fields
    // Fields private Assembly assembly = null;
    private Assembly assembly = null; private string className = "";
    private string className = ""; private object classObject = null;
    private object classObject = null; private SoapHttpClientProtocol classObjectSoapHttpClient = null;
    private SoapHttpClientProtocol classObjectSoapHttpClient = null; private Type classType = null;
    private Type classType = null;
 // Methods
    // Methods public bool BindingClass(Assembly assembly, string className)
    public bool BindingClass(Assembly assembly, string className)
 
     {
{ this.assembly = assembly;
        this.assembly = assembly; this.className = className;
        this.className = className; this.classType = this.assembly.GetType(this.className, true, true);
        this.classType = this.assembly.GetType(this.className, true, true); this.classObject = Activator.CreateInstance(this.classType);
        this.classObject = Activator.CreateInstance(this.classType); try
        try
 
         {
{ this.classObjectSoapHttpClient = this.classObject as SoapHttpClientProtocol;
            this.classObjectSoapHttpClient = this.classObject as SoapHttpClientProtocol; }
        } catch
        catch
 
         {
{ }
        } return true;
        return true; }
    }
 public DynWSMethodBinding BindingMethod(string methodName)
    public DynWSMethodBinding BindingMethod(string methodName)
 
     {
{ if ((this.assembly == null) || (this.classObject == null))
        if ((this.assembly == null) || (this.classObject == null))
 
         {
{ throw new Exception("请先绑定类。");
            throw new Exception("请先绑定类。"); }
        } DynWSMethodBinding binding = new DynWSMethodBinding();
        DynWSMethodBinding binding = new DynWSMethodBinding(); if (binding.BindingMethod(this, methodName))
        if (binding.BindingMethod(this, methodName))
 
         {
{ return binding;
            return binding; }
        } return null;
        return null; }
    }
 public void Dispose()
    public void Dispose()
 
     {
{ if (this.classObjectSoapHttpClient != null)
        if (this.classObjectSoapHttpClient != null)
 
         {
{ this.classObjectSoapHttpClient.Dispose();
            this.classObjectSoapHttpClient.Dispose(); }
        } }
    }
 public object GetFieldValue(string attName)
    public object GetFieldValue(string attName)
 
     {
{ if (this.classObject == null)
        if (this.classObject == null)
 
         {
{ throw new Exception("请先绑定类。");
            throw new Exception("请先绑定类。"); }
        } try
        try
 
         {
{ return this.classType.InvokeMember(attName, BindingFlags.GetProperty, null, this.classObject, new object[0]);
            return this.classType.InvokeMember(attName, BindingFlags.GetProperty, null, this.classObject, new object[0]); }
        } catch
        catch
 
         {
{ return this.classType.InvokeMember(attName, BindingFlags.GetField, null, this.classObject, new object[0]);
            return this.classType.InvokeMember(attName, BindingFlags.GetField, null, this.classObject, new object[0]); }
        } }
    }
 public bool SetFieldValue(string attName, object attValue)
    public bool SetFieldValue(string attName, object attValue)
 
     {
{ if (this.classObject == null)
        if (this.classObject == null)
 
         {
{ throw new Exception("请先绑定类。");
            throw new Exception("请先绑定类。"); }
        } try
        try
 
         {
{
 this.classType.InvokeMember(attName, BindingFlags.SetProperty, null, this.classObject, new object[]
            this.classType.InvokeMember(attName, BindingFlags.SetProperty, null, this.classObject, new object[] 
 { attValue });
{ attValue }); }
        } catch
        catch
 
         {
{
 this.classType.InvokeMember(attName, BindingFlags.SetField, null, this.classObject, new object[]
            this.classType.InvokeMember(attName, BindingFlags.SetField, null, this.classObject, new object[] 
 { attValue });
{ attValue }); }
        } return true;
        return true; }
    }
 // Properties
    // Properties public Assembly Assembly
    public Assembly Assembly
 
     {
{ get
        get
 
         {
{ return this.assembly;
            return this.assembly; }
        } }
    }
 public string ClassName
    public string ClassName
 
     {
{ get
        get
 
         {
{ return this.className;
            return this.className; }
        } }
    }
 public object ClassObject
    public object ClassObject
 
     {
{ get
        get
 
         {
{ return this.classObject;
            return this.classObject; }
        } }
    }
 public Type ClassType
    public Type ClassType
 
     {
{ get
        get
 
         {
{ return this.classType;
            return this.classType; }
        } }
    }
 public string Url
    public string Url
 
     {
{ get
        get
 
         {
{ return this.GetFieldValue("Url").ToString();
            return this.GetFieldValue("Url").ToString(); }
        } set
        set
 
         {
{ this.SetFieldValue("Url", value);
            this.SetFieldValue("Url", value); }
        } }
    } }
}
 
  [ClassInterface(ClassInterfaceType.AutoDual)]
 [ClassInterface(ClassInterfaceType.AutoDual)] public class DynWSMethodBinding : IDisposable
public class DynWSMethodBinding : IDisposable

 {
{ // Fields
    // Fields private DynWSClassBinding classBinding = null;
    private DynWSClassBinding classBinding = null; private MethodInfo methodInfo = null;
    private MethodInfo methodInfo = null; private string methodName = "";
    private string methodName = ""; private ParameterInfo[] methodParams = null;
    private ParameterInfo[] methodParams = null; private object[] methodParamsValue = null;
    private object[] methodParamsValue = null;
 // Methods
    // Methods public bool BindingMethod(DynWSClassBinding classBND, string methodName)
    public bool BindingMethod(DynWSClassBinding classBND, string methodName)
 
     {
{ this.classBinding = classBND;
        this.classBinding = classBND; this.methodName = methodName;
        this.methodName = methodName; this.methodInfo = this.classBinding.ClassType.GetMethod(this.methodName);
        this.methodInfo = this.classBinding.ClassType.GetMethod(this.methodName); if (this.methodInfo != null)
        if (this.methodInfo != null)
 
         {
{ this.methodParams = this.methodInfo.GetParameters();
            this.methodParams = this.methodInfo.GetParameters(); this.methodParamsValue = new object[this.methodParams.Length];
            this.methodParamsValue = new object[this.methodParams.Length]; return true;
            return true; }
        } return false;
        return false; }
    }
 public void ClearParamsValue()
    public void ClearParamsValue()
 
     {
{ this.methodParamsValue = new object[this.methodParams.Length];
        this.methodParamsValue = new object[this.methodParams.Length]; }
    }
 public void Dispose()
    public void Dispose()
 
     {
{ if (this.classBinding != null)
        if (this.classBinding != null)
 
         {
{ this.classBinding.Dispose();
            this.classBinding.Dispose(); }
        } }
    }
 private int GetParamIndex(string paramName)
    private int GetParamIndex(string paramName)
 
     {
{ if (this.methodParams != null)
        if (this.methodParams != null)
 
         {
{ foreach (ParameterInfo info in this.methodParams)
            foreach (ParameterInfo info in this.methodParams)
 
             {
{ if (info.Name.ToLower() == paramName.ToLower())
                if (info.Name.ToLower() == paramName.ToLower())
 
                
 {
{ return info.Position;
                    return info.Position; }
                } }
            } }
        } return -1;
        return -1; }
    }
 public ParameterInfo GetParamInfo(string paramName)
    public ParameterInfo GetParamInfo(string paramName)
 
     {
{ foreach (ParameterInfo info in this.methodParams)
        foreach (ParameterInfo info in this.methodParams)
 
         {
{ if (info.Name.ToLower() == paramName.ToLower())
            if (info.Name.ToLower() == paramName.ToLower())
 
             {
{ return info;
                return info; }
            } }
        } return null;
        return null; }
    }
 public string GetParamInfoString()
    public string GetParamInfoString()
 
     {
{ StringBuilder builder = new StringBuilder();
        StringBuilder builder = new StringBuilder(); if (this.methodParams != null)
        if (this.methodParams != null)
 
         {
{ int num = 0;
            int num = 0; foreach (ParameterInfo info in this.methodParams)
            foreach (ParameterInfo info in this.methodParams)
 
             {
{ object obj2 = (this.methodParamsValue != null) ? this.methodParamsValue[info.Position] : null;
                object obj2 = (this.methodParamsValue != null) ? this.methodParamsValue[info.Position] : null; string str = (obj2 == null) ? "<null>" : obj2.ToString();
                string str = (obj2 == null) ? "<null>" : obj2.ToString();
 builder.Append(string.Format("序号:{0}/{1} 参数:{2} 值:{3} 类型:{4} ", new object[]
                builder.Append(string.Format("序号:{0}/{1} 参数:{2} 值:{3} 类型:{4} ", new object[]  { num++, info.Position, info.Name, str, info.ParameterType }));
{ num++, info.Position, info.Name, str, info.ParameterType })); }
            } }
        } return builder.ToString();
        return builder.ToString(); }
    }
 public object Invoke()
    public object Invoke()
 
     {
{ return this.methodInfo.Invoke(this.classBinding.ClassObject, this.methodParamsValue);
        return this.methodInfo.Invoke(this.classBinding.ClassObject, this.methodParamsValue); }
    }
 public bool SetParamValue(string paramName, object paramValue)
    public bool SetParamValue(string paramName, object paramValue)
 
     {
{ try
        try
 
         {
{ object obj2 = null;
            object obj2 = null; foreach (ParameterInfo info in this.methodParams)
            foreach (ParameterInfo info in this.methodParams)
 
             {
{ if (info.Name.ToLower() == paramName.ToLower())
                if (info.Name.ToLower() == paramName.ToLower())
 
                 {
{ obj2 = Convert.ChangeType(paramValue, info.ParameterType);
                    obj2 = Convert.ChangeType(paramValue, info.ParameterType); this.methodParamsValue[info.Position] = obj2;
                    this.methodParamsValue[info.Position] = obj2; return true;
                    return true; }
                } }
            } return false;
            return false; }
        } catch
        catch
 
         {
{ return false;
            return false; }
        } }
    }
 // Properties
    // Properties public DynWSClassBinding ClassBinding
    public DynWSClassBinding ClassBinding
 
    {
{ get
        get
 
         {
{ return this.classBinding;
            return this.classBinding; }
        } }
    }
 public MethodInfo MethodInfo
    public MethodInfo MethodInfo
 
     {
{ get
        get
 
         {
{ return this.methodInfo;
            return this.methodInfo; }
        } }
    }
 public string MethodName
    public string MethodName
 
     {
{ get
        get
 
         {
{ return this.methodName;
            return this.methodName; }
        } }
    }
 public ParameterInfo[] Params
    public ParameterInfo[] Params
 
     {
{ get
        get
 
         {
{ return this.methodParams;
            return this.methodParams; }
        } }
    }
 public object[] ParamValues
    public object[] ParamValues
 
     {
{ get
        get
 
         {
{ return this.methodParamsValue;
            return this.methodParamsValue; }
        } }
    } }
}
 
 
使用介绍:

 Code
Code 使用介绍:
使用介绍:
 
 
 public bool CheckUser(string strUName, string strPwd)
 public bool CheckUser(string strUName, string strPwd)
 
         {
{ try
            try
 
             {
{ DynWebService ws = new DynWebService();
                DynWebService ws = new DynWebService(); ws.Binding(WSDLUrl, "", System.Runtime.InteropServices.ClassInterfaceType.None);
                ws.Binding(WSDLUrl, "", System.Runtime.InteropServices.ClassInterfaceType.None); DynWSClassBinding cls = ws.BindingClass("");
                DynWSClassBinding cls = ws.BindingClass(""); cls.Url = Url;
                cls.Url = Url; DynWSMethodBinding method = cls.BindingMethod("eomsAuthentication");
                DynWSMethodBinding method = cls.BindingMethod("eomsAuthentication"); method.SetParamValue("serSupplier", "");
                method.SetParamValue("serSupplier", ""); method.SetParamValue("serCaller", "");
                method.SetParamValue("serCaller", ""); method.SetParamValue("callerPwd", "");
                method.SetParamValue("callerPwd", ""); //pMethod->SetParamValue(_bstr_t("serSupplier"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Supplier")));
                //pMethod->SetParamValue(_bstr_t("serSupplier"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Supplier"))); //pMethod->SetParamValue(_bstr_t("serCaller"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Caller")));
                //pMethod->SetParamValue(_bstr_t("serCaller"),_variant_t((LPCTSTR)CGlobal::GetAppSetting("Caller"))); //CString strCallerPwd = (LPCTSTR)CGlobal::GetAppSetting("CallerPwd");
                //CString strCallerPwd = (LPCTSTR)CGlobal::GetAppSetting("CallerPwd"); //strCallerPwd = CEnc::DecodeAndAutoCheck(strCallerPwd);
                //strCallerPwd = CEnc::DecodeAndAutoCheck(strCallerPwd); string callTime = DateTime.Now.ToString("yyyy-mm-hh HH:MM:SS");
                string callTime = DateTime.Now.ToString("yyyy-mm-hh HH:MM:SS"); method.SetParamValue("callTime", callTime);
                method.SetParamValue("callTime", callTime); method.SetParamValue("userName", strUName);
                method.SetParamValue("userName", strUName); method.SetParamValue("userPassword", strPwd);
                method.SetParamValue("userPassword", strPwd);
 object ret = method.Invoke();
                object ret = method.Invoke(); string sret = ret == null ? "" : ret.ToString();
                string sret = ret == null ? "" : ret.ToString(); if (sret.Length == 0)
                if (sret.Length == 0)
 
                 {
{ return true;
                    return true; }
                } else
                else
 
                 {
{ throw new Exception("登录失败:" + sret);
                    throw new Exception("登录失败:" + sret); }
                } }
            } catch
            catch
 
             {
{ return false;
                return false; }
            } }
        }
有事Q我:
姓名:颜昌钢
联系方式:yanchanggang@boco.com.cn
电话:13886086508
QQ:95550107
公司:亿阳集团武汉分公司
移动飞信:647360243
 
                    
                     
                    
                 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号