我有一个字符串序列化实用程序,它接受(几乎)任何类型的变量并将其转换为字符串.因此,例如,根据我的惯例,整数值123将被序列化为"i:3:123"(i =整数; 3 =字符串的长度; 123 =值).
该实用程序处理所有原始类型,以及一些非泛型集合,如ArrayLists和Hashtables.界面是这种形式
public static string StringSerialize(object o) {}
在内部我检测对象是什么类型并相应地序列化它.
现在我想升级我的实用程序来处理泛型集合.有趣的是,我找不到一个合适的函数来检测对象是一个泛型集合,它包含哪些类型 - 我需要哪些信息才能正确序列化.到目前为止,我一直在使用表单的编码
if (o is int) {// do something}
但这似乎不适用于泛型.
您有什么推荐的吗?
编辑:感谢Lucero,我已经接近答案了,但我仍然坚持这个小小的语法难题:
if (t.IsGenericType) {
if (typeof(List<>) == t.GetGenericTypeDefinition()) {
Type lt = t.GetGenericArguments()[0];
List<lt> x = (List<lt>)o;
stringifyList(x);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码无法编译,因为" lt"不允许作为对象的<T>参数List<>.为什么不?什么是正确的语法?
我从这个问题开始,我在那里回答,现在我在这里问更基本的问题.我已将查询简化为:
var q = from ent in LinqUtils.GetTable<Entity>()
from tel in ent.Telephones.DefaultIfEmpty()
select new {
Name = ent.FormattedName,
Tel = tel != null ? tel.FormattedNumber : "" // this is what causes the error
};
Run Code Online (Sandbox Code Playgroud)
tel.FormattedNumber是一个将字段Number和Extension字段组合成一个整齐格式的字符串的属性.这是导致的错误:
System.InvalidOperationException: Could not translate expression 'Table(Entity).SelectMany(ent => ent.Telephones.DefaultIfEmpty(), (ent, tel) => new <>f__AnonymousType0`2(Name = ent.FormattedName, Tel = IIF((tel != null), tel.FormattedNumber, "")))' into SQL and could not treat it as a local expression.
Run Code Online (Sandbox Code Playgroud)
如果我将上面的参考更改为FormattedNumber …
我在SO上找到了一个很好的答案,描述了如何设置自定义用户角色,我在我的项目中也做了同样的事情.所以在我的登录服务中,我有:
public ActionResult Login() {
// password authentication stuff omitted here
var roles = GetRoles(user.Type); // returns a string e.g. "admin,user"
var authTicket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(20), // expiry
false,
roles,
"/");
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);
return new XmlResult(xmlDoc); // don't worry so much about this - returns XML as ActionResult
}
Run Code Online (Sandbox Code Playgroud)
在Global.asax.cs中,我(从另一个答案中逐字复制):
protected void Application_AuthenticateRequest(Object sender, EventArgs e) {
var authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null) {
var authTicket = …Run Code Online (Sandbox Code Playgroud) 使用数据库第一个模型:假设我们有经典的表Student,Course和StudentCourse(后者显然有FKS到Student和Course).
如果将此模型导入EF,您将获得为每个模型生成的对象.在Student和Course班将各自拥有的集合StudentCourses,从中你需要跳到另一个关系去的Course或Student分别.
我希望以这样的方式生成代码,使得底层交集表是不可见的,即Student具有集合Courses,并且Course具有集合Students.我已经在其他ORM软件(特别是TopLink)中看到了这一点.可以在EF中完成吗?
假设我想按国家/地区对客户数据库进行排名.在SQL中我会写:
select CountryID, CustomerCount = count(*),
[Rank] = RANK() over (order by count(*) desc)
from Customer
Run Code Online (Sandbox Code Playgroud)
现在我想在Entity Framework中写这个:
var ranks = db.Customers
.GroupBy(c => c.CountryID)
.OrderByDescending(g => g.Count())
.Select((g, index) => new {CountryID = g.Key, CustomerCount = g.Count, Rank = index+1});
Run Code Online (Sandbox Code Playgroud)
这有两个问题:
System.NotSupportedException; 显然,没有SQL转换的重载.Select()使用行号; .ToList()为了能够调用这个方法,你必须将所有内容都拉入内存中; 和RANK()SQL中的函数那样处理相同的排名,即它们应该具有相同的排名,然后下面的项目跳到原始顺序.那我该怎么做呢?
我经常发现如果我在Linq查询中有太多连接(无论是使用Entity Framework还是NHibernate)和/或生成的匿名类的形状太复杂,Linq需要很长时间才能将结果集具体化为对象.
这是一个通用的问题,但这是使用NHibernate的一个具体示例:
var libraryBookIdsWithShelfAndBookTagQuery = (from shelf in session.Query<Shelf>()
join sbttref in session.Query<ShelfBookTagTypeCrossReference>() on
shelf.ShelfId equals sbttref.ShelfId
join bookTag in session.Query<BookTag>() on
sbttref.BookTagTypeId equals (byte)bookTag.BookTagType
join btbref in session.Query<BookTagBookCrossReference>() on
bookTag.BookTagId equals btbref.BookTagId
join book in session.Query<Book>() on
btbref.BookId equals book.BookId
join libraryBook in session.Query<LibraryBook>() on
book.BookId equals libraryBook.BookId
join library in session.Query<LibraryCredential>() on
libraryBook.LibraryCredentialId equals library.LibraryCredentialId
join lcsg in session
.Query<LibraryCredentialSalesforceGroupCrossReference>()
on library.LibraryCredentialId equals lcsg.LibraryCredentialId
join userGroup in session.Query<UserGroup>() on
lcsg.UserGroupOrganizationId equals userGroup.UserGroupOrganizationId
where
shelf.ShelfId == shelfId && …Run Code Online (Sandbox Code Playgroud) 每次为单击一次的应用程序发布新更新时,app.config文件中的变量都将被销毁
<userSettings>
<app.My.MySettings>
<setting name="Email" serializeAs="String">
<value />
</setting>
<setting name="UserName" serializeAs="String">
<value />
</setting>
</app.My.MySettings>
</userSettings>
Run Code Online (Sandbox Code Playgroud)
我怎么能防止这种情况?
有没有办法从以前的应用程序版本中获取变量?
对于我正在构建的网站(针对移动用户),我正在考虑使用一些Ajax控件.我想知道哪些移动浏览器支持和不支持Javascript和Ajax,所以我知道我是否至少覆盖了我的大部分目标市场(即iPhone,Droid,Nokia,Opera).如果没有,我将不得不找到另一种表达形式的方式......
谢谢!
我只是在学习围绕WCF的绳索.我打算做的是使用NetTcpBinding在客户端和服务器之间打开双工通道,并使其无限期保持打开状态,以便服务器可以向客户端发起请求.
然后我偶然发现了Jesse Ezell撰写的这篇博客,这似乎表明保持渠道无限期开放是一件坏事,因为你无法发现错误,这会造成各种不稳定因素.
那是对的吗?如果我使用NetTcpBinding并在关系的任何一侧保持对开放通道的引用,那么如果通信失败会发生什么?我如何捕捉失败事件?还有其他什么问题?您使用的.NET框架有什么区别吗?(我在4.0.)
我正在用IIS7托管的C#/ ASP.NET中编写一个SaaS应用程序.我想为每个注册的客户创建一个个性化的子域名,即fred.mydomain.com,bob.mydomain.com,每个客户都指向同一个应用程序,每个客户只有不同的皮肤.
如何以编程方式创建这些子域?
c# ×7
asp.net ×3
linq ×2
.net-4.0 ×1
ajax ×1
asp.net-mvc ×1
browser ×1
clickonce ×1
deployment ×1
generics ×1
iis-7 ×1
javascript ×1
linq-to-sql ×1
mobile ×1
nhibernate ×1
performance ×1
saas ×1
subdomain ×1
typechecking ×1
wcf ×1