/// <summary>
/// 获取得到值以后的 泛型数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="SQL"></param>
/// <param name="list"></param>
/// <returns></returns>
public List<T> getModelList<T>(string SQL, List<string> list)where T:class,new()
{
try
{
List<T> Mlist = new List<T>();
IDataReader dr = getDataReader(SQL, list);
while (dr.Read())
{
T obj = new T();
obj = this.LoadFromReader<T>(dr);
Mlist.Add(obj);
}
return Mlist;
}
catch (Exception ex)
{
return null;
}
}WCF编译时,出现如下错误
类型“System.Collections.Generic.List`1[T]”无法作为架构类型被导出,因为它是一种开放的泛型类型。泛型类型仅可以在其所有的泛型参数类型均为实际类型时才可以被导出。 寻求解决办法
解决方案 »
- 未处理 FileNotFoundException
- C#如何增加监听器?
- 求解答tree view 空间知识?
- 请问这是什么原因?强类型DataSet的表被Fill后内容没了?
- 一个关于私有构造函数的问题!
- 代码重复率高,求优化代码
- datagrid绑定sql server中的视图(view),是否可以使用DataAdapter.Update()进行保存?
- 请问在.net(c#,winform)中如何引用vb开发的.dll???
- 小白有问 关于sql server2008 express
- 新安装。net2003,创建web程序出错。。
- 关于windowsform中的datagridview控件问题!!!
- WCF使用泛型方法的问题
要不要帮你把那个泛型方法改写成可以给WCF调用的非泛型方法形式,且功能不变?
public class ConManager
{
NetTcpBinding netTcpBinding = null;
ServiceEndpoint serviceEndpoint = null;
EndpointAddress endpointAddress = null;
InstanceContext instanceContext = null;
public DuplexChannelFactory<T> WCFconfig<T>(long maxReceivedMessageSize, TransferMode transferMode)
{
netTcpBinding = new NetTcpBinding();
netTcpBinding.MaxReceivedMessageSize = maxReceivedMessageSize;
netTcpBinding.TransferMode = transferMode;
ContractDescription cd = ContractDescription.GetContract(typeof(T)); serviceEndpoint = new ServiceEndpoint(cd);
serviceEndpoint.Address = new EndpointAddress("");
serviceEndpoint.Binding = netTcpBinding;
instanceContext = new InstanceContext(this);
DuplexChannelFactory<T> factory = new DuplexChannelFactory<T>(instanceContext, serviceEndpoint);
return factory;
}
}
{
try
{
T obj = new T();
Type type = obj.GetType();
for (int i = 0; i < reader.FieldCount; i++)
{
PropertyInfo prop = type.GetProperty(reader.GetName(i));
if (prop != null && reader.GetValue(i) != DBNull.Value)
{
try
{
prop.SetValue(obj, reader.GetValue(i), null);
}
catch (Exception)
{
continue;
}
}
}
return obj;
}
catch
{
return null;
}
}
using System.Reflection;namespace showjim.setup.cSharp
{
[showjim.setup.attribute.serialize(isMemberEnum = false)]
public partial class type
{
public string name;
public string assemblyName;
public Type value
{
get
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.FullName == assemblyName)
{
Type type = assembly.GetType(name);
if (type != null) return type;
}
}
showjim.sys.debug.message("未能加载类型 : " + name + " in " + assemblyName).throwException();
return null;
}
}
public type(Type type)
{
name = type.FullName;
assemblyName = type.Assembly.FullName;
}
}
}模板里面的参数生成 _genericTypes_ = new showjim.setup.cSharp.type[]
{
#region LOOP genericParameters
new showjim.setup.cSharp.type(typeof(@genericParameterTypeName))/*NOT:isLast*/,/*NOT:isLast*/
#endregion LOOP genericParameters
},模板里面的最终调用 #region IF method.IsGenericMethod
private static System.Reflection.MethodInfo @genericIndexName = showjim.sys.reflection.methodInfo.getGenericDefinition<showjim.setup.attribute.tcpCall>(typeof(@codeType.fullName).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic), method => method.IsGenericMethod && method.Name == "@methodName" /*IF:isMethodReturn*/ && method.ReturnType != typeof(void)/*IF:isMethodReturn*//*Not:isMethodReturn*/ && method.ReturnType == typeof(void)/*Not:isMethodReturn*/ && method.GetParameters().Length == @methodParameterCount);
public static object @methodIndexName(Type[] types, params object[] parameters)
{
if (@genericIndexName == null) showjim.sys.debug.message(null).throwException();
return @genericIndexName/**/.MakeGenericMethod(types).Invoke(null, parameters);
}
#endregion IF method.IsGenericMethod对于函数我没有处理相同参数数量重载的情况,因为现在还没用到。
{
try
{
IList Mlist = new List<object>();
IDataReader dr = getDataReader(SQL, list);
while (dr.Read())
{
object obj = this.GetType().GetMethods().First((p) => p.IsGenericMethod && p.Name == "LoadFromReader").MakeGenericMethod(Helper.types[type]).Invoke(this, new object[] { dr });
Mlist.Add(obj);
}
return Mlist;
}
catch (Exception ex)
{
return null;
}
}接口:
[OperationContract]
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
IList getModelList(string type, string SQL, List<string> list);
Helper类的定义(这是关键): public static class Helper
{
public static Dictionary<string, Type> types = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "XXX.Model.dll").GetTypes().ToDictionary(t => t.Name); public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
return types.Select(t=>t.Value);
}
}
上面的Helper类,调用了一个实体类的程序集XXX.Model.dll,你必须要将所有实体类单独做到一个项目中并编译到dll文件,然后通过dll文件名加载里面所有类型,否则就需要手动添加一个个实体类到那个types变量中。
这两天我身体也不好在调休,没能及时回复,还是谢谢您能抽空给我解答,上面的代码我刚看了下,我觉得那个实体类独立编译成dll不现实,我写这个方法的目的就是希望在添加新的实体类的时候不需要去改动服务端的代码
小看WCF?在我眼里,WCF还真不是什么好东西。
你的意思是说网络调用支持泛型反而是不应该的了?支持部分序列化也是不应该的?可笑
{
return a;
}泛型函数调用的全写是
int a = 1;
a = Get<int>(a);简写
int a = 1;
a = Get(a);不要被简写搞迷惑了。
{
#region LOOP genericParameters
new showjim.setup.cSharp.type(typeof(@genericParameterTypeName))/*NOT:isLast*/,/*NOT:isLast*/
#endregion LOOP genericParameters
},
17#那段模板你看不明白,那就上一段生成好的测试代码给你看
_genericTypes_ = new showjim.setup.cSharp.type[]
{
new showjim.setup.cSharp.type(typeof(valueType)),
new showjim.setup.cSharp.type(typeof(valueType1))
},
这段才看明白的,因为你的命名规则实在别扭,第一次见到用@符号开头的变量名,所以一直被我pass了。
我也不用WCF,在我看来,那个操作契约就是多余的工作。下面分别是泛型的同步于异步调用。namespace showjim.setup.cSharp.test
{
[showjim.setup.attribute.tcpCall(server = showjim.config.tcpCall.server.enumType.dataProxy)]
public partial class TcpCall
{
[showjim.setup.attribute.tcpCall]
private static valueType generic<valueType, valueType1>(valueType value, ref valueType1 refValue, out valueType outValue)
{
return outValue = value;
}
[showjim.setup.attribute.tcpCall]
private static void generic<valueType, valueType1>(showjim.setup.coder.AsyncMethodParameter<valueType1> onMethod, valueType value, ref valueType1 refValue, out valueType outValue)
{
outValue = value;
}
}
} int a = 0, c;
string b = null;
int d = showjim.setup.cSharp.test.TcpCall.tcpCall.client.generic(a, ref b, out c);
缺点是通讯机制少(目前只支持TCP),我一直只用TCP,我是用到什么写什么。
中 LoadFromReader 啥意思?