public
class
AjaxClass
{
private
string
_Script;
private
Dictionary
<
string
, AjaxMethodInfo
>
_AjaxMethods;
private
List
<
PropertyInfo
>
_AjaxProperties;
///
<summary>
///
利用反射,创建js脚本
///
</summary>
///
<param name="type"></param>
public
AjaxClass(Type type)
{
List
<
string
>
scripts
=
new
List
<
string
>
();
Dictionary
<
string
, AjaxMethodInfo
>
methodList
=
new
Dictionary
<
string
, AjaxMethodInfo
>
();
List
<
PropertyInfo
>
propertyList
=
new
List
<
PropertyInfo
>
();
MethodInfo[] methods;
PropertyInfo[] properties;
string
clientName;
object
[] attrs
=
type.GetCustomAttributes(
typeof
(AjaxAttribute),
false
);
if
(attrs.Length
>
0
)
{
AjaxAttribute attr
=
attrs[
0
]
as
AjaxAttribute;
clientName
=
string
.IsNullOrEmpty(attr.Name)
?
type.Name : attr.Name;
if
(attr.Inherited)
{
methods
=
type.GetMethods(BindingFlags.Public
|
BindingFlags.Instance);
properties
=
type.GetProperties(BindingFlags.Public
|
BindingFlags.Instance
|
BindingFlags.GetProperty);
}
else
{
methods
=
type.GetMethods(BindingFlags.Public
|
BindingFlags.Instance
|
BindingFlags.DeclaredOnly);
properties
=
type.GetProperties(BindingFlags.Public
|
BindingFlags.Instance
|
BindingFlags.GetProperty
|
BindingFlags.DeclaredOnly);
}
}
else
{
clientName
=
type.Name;
methods
=
type.GetMethods(BindingFlags.Public
|
BindingFlags.Instance
|
BindingFlags.DeclaredOnly);
properties
=
type.GetProperties(BindingFlags.Public
|
BindingFlags.Instance
|
BindingFlags.GetProperty
|
BindingFlags.DeclaredOnly);
}
foreach
(MethodInfo mi
in
methods)
{
attrs
=
mi.GetCustomAttributes(
typeof
(AjaxAttribute),
false
);
if
(attrs.Length
>
0
)
{
AjaxReturnType returnType
=
AjaxReturnType.Html;
if
(mi.ReturnType
==
typeof
(
int
)
||
mi.ReturnType
==
typeof
(System.Web.Mvc.JsonResult))
returnType
=
AjaxReturnType.Json;
//
AjaxReturnType returnType = (attrs[0] as AjaxAttribute).ReturnType;
AjaxMethodInfo ami
=
new
AjaxMethodInfo(mi, returnType);
methodList.Add(mi.Name, ami);
scripts.Add(BuildAjaxRequest(ami));
}
}
foreach
(PropertyInfo pi
in
properties)
{
attrs
=
pi.GetCustomAttributes(
typeof
(AjaxAttribute),
false
);
if
(attrs
!=
null
&&
attrs.Length
>
0
) propertyList.Add(pi);
}
if
(methodList.Count
>
0
) _AjaxMethods
=
methodList;
if
(propertyList.Count
>
0
) _AjaxProperties
=
propertyList;
BuildScript(clientName, scripts, propertyList);
}
///
<summary>
///
输入所有属性的js脚本
///
</summary>
///
<param name="obj"></param>
///
<returns></returns>
public
string
GetScript(
object
obj)
{
if
(_AjaxProperties
==
null
)
return
_Script;
string
script
=
string
.Empty;
foreach
(PropertyInfo pi
in
_AjaxProperties)
{
if
(script
==
string
.Empty) script
=
BuildAjaxObject(obj, pi);
else
script
+=
"
,\r\n
"
+
BuildAjaxObject(obj, pi);
}
return
_Script.Replace(
"
{property}
"
, script);
}
///
<summary>
///
创建最终的js脚本
///
</summary>
///
<param name="typeName"></param>
///
<param name="scripts"></param>
///
<param name="propertyList"></param>
private
void
BuildScript(
string
typeName, List
<
string
>
scripts, List
<
PropertyInfo
>
propertyList)
{
if
(scripts.Count
>
0
||
propertyList.Count
>
0
)
{
StringBuilder sb
=
new
StringBuilder();
sb.AppendLine();
sb.AppendLine(
"
<script type=\
"
text
/
javascript\
"
>
"
);
sb.AppendFormat(
"
var {0} = {{
"
, typeName);
if
(propertyList.Count
>
0
)
{
sb.AppendLine();
sb.Append(
"
{property}
"
);
}
for
(
int
i
=
0
; i
<
scripts.Count; i
++
)
{
if
(i
==
0
&&
propertyList.Count
==
0
) sb.AppendLine();
else
sb.AppendLine(
"
,
"
);
sb.Append(
"
"
+
scripts[i]);
}
sb.AppendLine();
sb.AppendLine(
"
}
"
);
sb.AppendLine(
"
</script>
"
);
_Script
=
sb.ToString();
}
}
///
<summary>
///
jquery相关ajax方法的脚本构建
///
</summary>
///
<param name="ami"></param>
///
<returns></returns>
private
string
BuildAjaxRequest(AjaxMethodInfo ami)
{
string
methodName
=
ami.MethodInfo.Name;
string
url
=
"
{url}
"
+
methodName
+
"
{querystring}
"
;
ParameterInfo[] parameters
=
ami.MethodInfo.GetParameters();
AjaxReturnType returnType
=
ami.ReturnType;
string
param, data;
if
(parameters
==
null
||
parameters.Length
==
0
)
{
param
=
"
callback
"
;
data
=
string
.Empty;
}
if
(parameters.Length
==
0
)
{
return
string
.Format(
@"
{0}: function(callback)
{{
$.getJSON('{1}', callback);
}}
"
,
methodName, url);
}
else
{
string
[] paramArray
=
new
string
[parameters.Length
+
1
];
string
[] dataArray
=
new
string
[parameters.Length];
for
(
int
i
=
0
; i
<
parameters.Length; i
++
)
{
paramArray[i]
=
parameters[i].Name;
dataArray[i]
=
string
.Format(
"
{0}:{0}
"
, parameters[i].Name);
}
//
paramArray[parameters.Length] = "callback";
param
=
string
.Join(
"
,
"
, paramArray);
param
=
param.Trim ().TrimEnd(
'
,
'
);
data
=
string
.Join(
"
,
"
, dataArray);
}
return
string
.Format(
@"
{0}: function({1},callback)
{{
$.getJSON('{2}',{{{3}}}, callback);
}}
"
,
methodName,param, url,data);
}
private
string
BuildAjaxObject(
object
obj, PropertyInfo pi)
{
object
value
=
pi.GetValue(obj,
null
);
object
[] attrs
=
pi.GetCustomAttributes(
typeof
(AjaxAttribute),
false
);
if
(attrs.Length
>
0
)
{
AjaxAttribute attr
=
attrs[
0
]
as
AjaxAttribute;
if
(attr.ReturnType
==
AjaxReturnType.Json
&&
value
is
string
)
return
string
.Format(
@"
{0}: {1}
"
, pi.Name, value);
}
StringBuilder sb
=
new
StringBuilder();
JsonWriter jw
=
new
JsonWriter(sb);
jw.Write(value);
return
string
.Format(
@"
{0}: {1}
"
, pi.Name, sb.ToString());
}
}