您的位置:首页 > 移动开发 > 微信开发

微信开发 企业号(三)-- 主动调用(通讯录、多媒体文件、自定义菜单,下发信息)

2014-10-01 13:56 555 查看
已完成通讯录、多媒体文件、自定义菜单,下发信息接口代码

代码结构

结构及父类CodeMap

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Ping.Helper
{
public class HttpHelper
{
public bool Debug { get; set; }
public CookieCollection Cookies
{
get { return _cookies; }
}

public void ClearCookies()
{
_cookies = new CookieCollection();
}

CookieCollection _cookies = new CookieCollection();

private static readonly string DefaultUserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
/// <summary>
/// 创建GET方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public HttpWebResponse CreateGetHttpResponse(string url, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
if (Debug)
{
Console.Write("Start Get Url:{0}    ", url);
}

if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}

HttpWebRequest request;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}

request.Method = "GET";
request.Headers["Pragma"] = "no-cache";
request.Accept = "text/html, application/xhtml+xml, */*";
request.Headers["Accept-Language"] = "en-US,en;q=0.5";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = DefaultUserAgent;
request.Referer = Referer;

if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}

if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value * 1000;
}
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
else
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(Cookies);
}
var v = request.GetResponse() as HttpWebResponse;

Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host)));
Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host)));
Cookies.Add(v.Cookies);

if (Debug)
{
Console.WriteLine("OK");
}

return v;
}

/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, Encoding requestEncoding, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
if (Debug)
{
Console.Write("Start Post Url:{0}  ", url);

foreach (KeyValuePair<string, string> keyValuePair in parameters)
{
Console.Write(",{0}:{1}", keyValuePair.Key, keyValuePair.Value);
}
}

if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01";
request.Referer = Referer;
request.Headers["Accept-Language"] = "en-US,en;q=0.5";
request.UserAgent = DefaultUserAgent;
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["Pragma"] = "no-cache";

if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}

if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
else
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(Cookies);
}

if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}

if (timeout.HasValue)
{
request.Timeout = timeout.Value * 1000;
}

request.Expect = string.Empty;

//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
var buffer = CraeteParameter(parameters);
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
var v = request.GetResponse() as HttpWebResponse;

Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host)));
Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host)));

Cookies.Add(v.Cookies);

if (Debug)
{
Console.WriteLine("OK");
}

return v;
}

/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public HttpWebResponse CreatePostHttpResponse(string url, string parameters, Encoding requestEncoding, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
if (Debug)
{
Console.Write("Start Post Url:{0} ,parameters:{1}  ", url, parameters);

}

if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}

request.Method = "POST";
request.Headers.Add("Accept-Language", "zh-CN,en-GB;q=0.5");
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.Referer = Referer;
request.Headers["Accept-Language"] = "en-US,en;q=0.5";
request.UserAgent = DefaultUserAgent;
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["Pragma"] = "no-cache";

if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
else
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(Cookies);
}

if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}

if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}

if (timeout.HasValue)
{
request.Timeout = timeout.Value * 1000;
}

request.Expect = string.Empty;

//如果需要POST数据
if (!string.IsNullOrEmpty(parameters))
{
byte[] data = requestEncoding.GetBytes(parameters);
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}

var v = request.GetResponse() as HttpWebResponse;

Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host)));
Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host)));
Cookies.Add(v.Cookies);

if (Debug)
{
Console.WriteLine("OK");
}

return v;
}

/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public HttpWebResponse CreatePostFileHttpResponse(string url, string filePath, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
if (Debug)
{
Console.Write("Start Post Url:{0}  ", url);

}

if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}

HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01";
request.Referer = Referer;
request.Headers["Accept-Language"] = "en-US,en;q=0.5";
request.UserAgent = DefaultUserAgent;
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["Pragma"] = "no-cache";

if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}

if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
else
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(Cookies);
}

if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}

if (timeout.HasValue)
{
request.Timeout = timeout.Value * 1000;
}

request.Expect = string.Empty;

//如果需要POST数据
if (!string.IsNullOrEmpty(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
BinaryReader r = new BinaryReader(fs);

//时间戳
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");

//请求头部信息
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(fs.Name);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);

request.ContentType = "multipart/form-data; boundary=" + strBoundary;
long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;

request.ContentLength = length;

//开始上传时间
DateTime startTime = DateTime.Now;

byte[] filecontent = new byte[fs.Length];

fs.Read(filecontent, 0, filecontent.Length);

using (Stream stream = request.GetRequestStream())
{

//发送请求头部消息
stream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

stream.Write(filecontent, 0, filecontent.Length);

//添加尾部的时间戳
stream.Write(boundaryBytes, 0, boundaryBytes.Length);
}

}
}
var v = request.GetResponse() as HttpWebResponse;

Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host)));
Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host)));

Cookies.Add(v.Cookies);

if (Debug)
{
Console.WriteLine("OK");
}

return v;
}

public static string CraeteParameter(IDictionary<string, string> parameters)
{
StringBuilder buffer = new StringBuilder();
foreach (string key in parameters.Keys)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
return buffer.ToString().TrimStart('&');
}

private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}

public string Post(string url, IDictionary<string, string> parameters, Encoding requestEncoding, Encoding responseEncoding, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
HttpWebResponse response = CreatePostHttpResponse(url, parameters, requestEncoding, timeout, userAgent, cookies, Referer, headers);

try
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), responseEncoding))
{
return reader.ReadToEnd();
}
}
catch (Exception)
{
return null;
}
}

public string Post(string url, string parameters, Encoding requestEncoding, Encoding responseEncoding, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
HttpWebResponse response = CreatePostHttpResponse(url, parameters, requestEncoding, timeout, userAgent, cookies, Referer, headers);

try
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), responseEncoding))
{
return reader.ReadToEnd();
}
}
catch (Exception)
{
return null;
}
}

public string PostFile(string url, string filePath, Encoding responseEncoding,
int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "",
Dictionary<string, string> headers = null)
{
HttpWebResponse response = CreatePostFileHttpResponse(url, filePath, timeout, userAgent, cookies, Referer,
headers);

try
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), responseEncoding))
{
return reader.ReadToEnd();
}
}
catch (Exception)
{
return null;
}
}

public string Get(string url, Encoding responseEncoding, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
HttpWebResponse response = CreateGetHttpResponse(url, timeout, userAgent, cookies, Referer, headers);

try
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), responseEncoding))
{
return reader.ReadToEnd();
}
}
catch (Exception)
{
return null;
}
}

public byte[] GetFile(string url, out Dictionary<string, string> header, int? timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary<string, string> headers = null)
{
HttpWebResponse response = CreateGetHttpResponse(url, timeout, userAgent, cookies, Referer, headers);

header = new Dictionary<string, string>();

foreach (string key in response.Headers.AllKeys)
{
header.Add(key, response.Headers[key]);
}

try
{
System.IO.Stream st = response.GetResponseStream();

byte[] by = new byte[response.ContentLength];

st.Read(by, 0, by.Length);

st.Close();

return by;
}
catch (Exception)
{
return null;
}

return null;
}
}
}


HttpHelper类

IHttpSend接口声明,抽象出同一交互接口

public interface IHttpSend
{
string Send(string url, string data);
}


代码中Http请求分为4中:普通Get请求,普通Post请求,Post文件上传请求,Get文件下载请求

并且不同请求中,Send方法第二个参数Data包含内容不同

//普通Get请求
public class HttpGetRequest : IHttpSend
{
public string Send(string url, string data)
{
return new HttpHelper().Get(url, Encoding.UTF8);
}
}

//普通Post请求
public class HttpPostRequest : IHttpSend
{
public string Send(string url, string data)
{
return new HttpHelper().Post(url, data, Encoding.UTF8, Encoding.UTF8);
}
}


//Post文件上传请求
class HttpPostFileRequest : IHttpSend
{
public string Send(string url, string data)
{
return new HttpHelper().PostFile(url, data,Encoding.UTF8);
}
}

//Get文件下载请求
class HttpGetFileRequest : IHttpSend
{
public string Send(string url, string path)
{
Dictionary<string, string> header;

var bytes = new HttpHelper().GetFile(url, out header);

if (header["Content-Type"].Contains("application/json"))
{
return Encoding.UTF8.GetString(bytes);
}
else
{
Regex regImg = new Regex("\"(?<fileName>.*)\"", RegexOptions.IgnoreCase);

MatchCollection matches = regImg.Matches(header["Content-disposition"]);

string fileName = matches[0].Groups["fileName"].Value;

string filepath = path.TrimEnd('\\') + "\\" + fileName;

System.IO.Stream so = new System.IO.FileStream(filepath, System.IO.FileMode.Create);

so.Write(bytes, 0, bytes.Length);

so.Close();
}

return JsonConvert.SerializeObject(header);
}
}


与微信交互

构建微信返回结果基类

public class OperationResultsBase
{
public int errcode { get; set; }
public string errmsg { get; set; }
}


构建统一发送接口,并使用协变,方便构造统一队列

public interface ISend<out T>
where T : OperationResultsBase, new()
{
T Send();
}


构建微信发送基类

public abstract class OperationRequestBase<T, THttp> : ISend<T>
where T : OperationResultsBase, new()
where THttp : IHttpSend, new()
{
protected abstract string Url();

/// <summary>
/// 视同attribute进行简单校验
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
private bool Verify(out string message)
{
message = "";
foreach (var pro in this.GetType().GetProperties())
{
var v = pro.GetCustomAttributes(typeof(IVerifyAttribute), true);

foreach (IVerifyAttribute verify in pro.GetCustomAttributes(typeof(IVerifyAttribute), true))
{
if (!verify.Verify(pro.PropertyType, pro.GetValue(this), out message))
{
return false;
}
}
}

return true;
}

/// <summary>
/// 格式化URL,替换Token
/// </summary>
/// <returns></returns>
protected string GetUrl()
{
if (Token.IsTimeOut())
{
Token.GetNewToken();
}

string url = Url();

if (url.Contains("=ACCESS_TOKEN"))
{
url = url.Replace("=ACCESS_TOKEN", "=" + Token.GetToken());
}

return url;
}

/// <summary>
/// 发送
/// </summary>
/// <returns></returns>
public T Send()
{
string message = "";
if (!Verify(out message))
{
throw new Exception(message);
}

//string result = new HttpHelper().Post(url, JsonConvert.SerializeObject(this), Encoding.UTF8, Encoding.UTF8);

IHttpSend httpSend = new THttp();

string result = HttpSend(httpSend, GetUrl());

return GetDeserializeObject(result);
}

/// <summary>
/// 处理返回结果
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
protected virtual T GetDeserializeObject(string result)
{
return JsonConvert.DeserializeObject<T>(result);
}

/// <summary>
/// 处理发送请求
/// </summary>
/// <param name="httpSend"></param>
/// <param name="url"></param>
/// <returns></returns>
protected virtual string HttpSend(IHttpSend httpSend, string url)
{
return httpSend.Send(url, JsonConvert.SerializeObject(this));
}

}


基类构造好,后续大部分开发就非常简单了,

需要先构建结果类(如果无特殊字段,可直接使用OperationResultsBase)

构建请求类,继承OperationRequestBase<>类,选择结果类,及发送方式,再构造URL,最后填入所需字段即可

如:开发“部门列表数据"接口

//先构造返回类
public class DepartmentListResult :OperationResultsBase
{
/// <summary>
/// 部门列表数据。以部门的order字段从小到大排列
/// </summary>
/// <returns></returns>
public List<DepartmentItem> department { get; set; }

public class DepartmentItem
{
/// <summary>
/// 部门id
/// </summary>
/// <returns></returns>
public string id { get; set; }

/// <summary>
/// 部门名称
/// </summary>
/// <returns></returns>
public string name { get; set; }

/// <summary>
/// 父亲部门id。根部门为1
/// </summary>
/// <returns></returns>
public string parentid { get; set; }
}
}

//再构造请求类
public class DepartmentList : OperationRequestBase<DepartmentListResult,HttpPostRequest>
{
protected override string Url()
{
return "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN";
}
}


如:开发“创建部门"接口

//返回值类
无

//请求类
/// <summary>
/// 创建部门
/// </summary>
public class DepartmentCreate : OperationRequestBase<OperationResultsBase,HttpPostRequest>
{
protected override string Url()
{
return "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN";
}

/// <summary>
/// 部门名称。长度限制为1~64个字符
/// </summary>
/// <returns></returns>
[Length(1,64)]
[IsNotNull]
public string name { get; set; }

/// <summary>
/// 父亲部门id。根部门id为1
/// </summary>
/// <returns></returns>
[IsNotNull]
public string parentid { get; set; }

/// <summary>
/// 在父部门中的次序。从1开始,数字越大排序越靠后
/// </summary>
/// <returns></returns>
public string order { get; set; }
}


验证

最后在加入加单验证机制,如不为空验证,长度限制

public interface IVerifyAttribute
{
bool Verify(Type type, object obj,out string message);
}

public class IsNotNullAttribute : Attribute, IVerifyAttribute
{
bool IsNotNull { get; set; }

string Message { get; set; }

public IsNotNullAttribute()
{
IsNotNull = true;
Message = "不能为空";
}

public IsNotNullAttribute(bool isNotNull)
{
IsNotNull = isNotNull;
Message = "不能为空";
}
public IsNotNullAttribute(bool isNull, string message)
{
IsNotNull = isNull;
Message = message;
}

public bool Verify(Type type, object obj, out string message)
{
message = "";

if (IsNotNull == false)
{
return true;
}

if (obj == null)
{
message = Message;
return false;
}

if (obj is IList)
{

if ((obj as IList).Count <= 0)
{
message = Message;
return false;
}
}

return true;
}
}

public class LengthAttribute : Attribute, IVerifyAttribute
{
int MinLength { get; set; }

int MaxLength { get; set; }

string Message { get; set; }

public LengthAttribute(int minLength, int maxLength)
{
MinLength = minLength;
MaxLength = maxLength;
Message = string.Format("字符串长度应在{0}到{1}之间",minLength,maxLength);
}
public LengthAttribute(int minLength, int maxLength,string message)
{
MinLength = minLength;
MaxLength = maxLength;
Message = string.Format(message, minLength, maxLength);
}

public bool Verify(Type type,object obj,out string message)
{
message = "";

if (type == typeof(string) && obj != null)
{
if ((obj as string).Length > MaxLength || (obj as string).Length < MinLength)
{
message = Message;
return false;
}
}

return true;
}
}


附上代码(仅通过测试,并未对异常处理)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐