中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

公司在網(wǎng)上做網(wǎng)站怎么做賬怎么注冊(cè)網(wǎng)站平臺(tái)

公司在網(wǎng)上做網(wǎng)站怎么做賬,怎么注冊(cè)網(wǎng)站平臺(tái),谷歌建站多少錢(qián),dede 手機(jī)網(wǎng)站插件一、什么是表達(dá)式目錄樹(shù) (1)Expression我們稱(chēng)為是表達(dá)式樹(shù),是一種數(shù)據(jù)結(jié)構(gòu)體,用于存儲(chǔ)需要計(jì)算,運(yùn)算的一種結(jié)構(gòu),這種結(jié)構(gòu)可以只是存儲(chǔ),而不進(jìn)行運(yùn)算。通常表達(dá)式目錄樹(shù)是配合Lambda一起來(lái)使用的…

一、什么是表達(dá)式目錄樹(shù)

(1)Expression我們稱(chēng)為是表達(dá)式樹(shù),是一種數(shù)據(jù)結(jié)構(gòu)體,用于存儲(chǔ)需要計(jì)算,運(yùn)算的一種結(jié)構(gòu),這種結(jié)構(gòu)可以只是存儲(chǔ),而不進(jìn)行運(yùn)算。通常表達(dá)式目錄樹(shù)是配合Lambda一起來(lái)使用的,lambda可以是匿名方法,當(dāng)然也可以使用Expression來(lái)動(dòng)態(tài)的創(chuàng)建!

二、Func與Expression的區(qū)別

1、Func是方法

 Func<int, int, int> func = (m, n) => m * n + 2;Console.WriteLine(func.Invoke(1, 1)); 
//運(yùn)算:1*1+2=3

?2、Expression是數(shù)據(jù)結(jié)構(gòu)

//lambda表達(dá)式聲明表達(dá)式目錄樹(shù)
Expression<Func<int, int, int>> exp = (m, n) => m * n + 2;
int result = exp.Compile().Invoke(1, 2);
Console.WriteLine(result); 
//運(yùn)算:1*2+2=4

注意:Expression只能為1行(如下圖會(huì)報(bào)錯(cuò))

3、使用ILSpy反編譯解析看一下

調(diào)一下格式更好看一點(diǎn)

            ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "m");ParameterExpression parameterExpression2 = Expression.Parameter(typeof(int), "n");var multiply = Expression.Multiply(parameterExpression, parameterExpression2);var constant = Expression.Constant(2,typeof(int));var add = Expression.Add(multiply, constant);Expression<Func<int, int, int>> exp =Expression.Lambda<Func<int, int, int>>(add,new ParameterExpression[2]{parameterExpression, parameterExpression2});
打印看看結(jié)果
      int result = exp.Compile().Invoke(11, 12);Console.WriteLine(result);

得到134,與m*n+2得出結(jié)果一致

4、拼裝練習(xí)

(1)練習(xí)一:

(2)練習(xí)二

(3)練習(xí)三

5、動(dòng)態(tài)生成硬編碼(通用、性能好)

需求:我希望復(fù)制一個(gè)People出來(lái)

    public class People {public int Age;public string Name;}public class PeopleCopy {public int Age;public string Name;}

方法1:通過(guò)硬編碼直接賦值

People people = new People()
{Age = 18,Name = "吳彥祖"
};
PeopleCopy peopleCopy = new PeopleCopy()
{Age = people.Age,Name = people.Name,
};

方法2:通過(guò)反射賦予

方法3:通過(guò)Json序列化與反序列化賦值

第一種方法性能最好,但是不夠通用。方法2和方法3性能不好。

方法4:

這時(shí)候可以考慮使用表達(dá)式目錄樹(shù)來(lái)動(dòng)態(tài)生成硬編碼

思路:用表達(dá)目錄樹(shù)動(dòng)態(tài)生成硬編碼,生成保存到字典里,下次再調(diào)用的時(shí)候則直接從字典里拿。

 public class ExpressionMapper{private static Dictionary<string, object> _dic = new Dictionary<string, object>();public static TOut Trans<TIn, TOut>(TIn tIn) {string key = string.Format("funckey_{0}_{1}", typeof(TIn).FullName, typeof(TOut).FullName);if (!_dic.ContainsKey(key)){ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");List<MemberBinding> memberBindingList = new List<MemberBinding>();foreach (var item in typeof(TOut).GetProperties()){MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));MemberBinding memberBinding = Expression.Bind(item, property);memberBindingList.Add(memberBinding);}foreach (var item in typeof(TOut).GetFields()){MemberExpression field = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));MemberBinding memberBinding = Expression.Bind(item, field);memberBindingList.Add(memberBinding);}MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]{parameterExpression});_dic[key] = lambda.Compile();}return ((Func<TIn, TOut>)_dic[key]).Invoke(tIn);}

方法5:泛型緩存(相比方法4可以節(jié)省讀取字典時(shí)候的消耗)

 public class ExpressionGenericMapper<TIn, TOut>{private static Func<TIn, TOut> _Func;static ExpressionGenericMapper(){ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");List<MemberBinding> memberBindingList = new List<MemberBinding>();foreach (var item in typeof(TOut).GetProperties()){MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));MemberBinding memberBinding = Expression.Bind(item, property);memberBindingList.Add(memberBinding);}foreach (var item in typeof(TOut).GetFields()){MemberExpression field = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));MemberBinding memberBinding = Expression.Bind(item, field);memberBindingList.Add(memberBinding);}MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]{parameterExpression});_Func = lambda.Compile();}public static TOut Trans(TIn t) {return _Func(t);}

看一下字典緩存和泛型類(lèi)緩存消耗的時(shí)間,明顯可以看到通過(guò)泛型類(lèi)緩存的性能更好,因?yàn)楣?jié)省了查找字典的性能消耗。

         PrintExecutionTime(() =>{Console.WriteLine("通過(guò)字典緩存,第一次消耗時(shí)間:");PeopleCopy copy = ExpressionMapper.Trans<People, PeopleCopy>(new People() { Age = 10, Name = "哇哈哈" });});PrintExecutionTime(() =>{Console.WriteLine("通過(guò)字典緩存,第二次消耗時(shí)間:");PeopleCopy copy2 = ExpressionMapper.Trans<People, PeopleCopy>(new People() { Age = 10, Name = "哇哈哈" });});PrintExecutionTime(() =>{Console.WriteLine("通過(guò)泛型類(lèi)緩存,第一次消耗時(shí)間:");PeopleCopy copy3 = ExpressionGenericMapper<People, PeopleCopy>.Trans(new People() { Age = 11, Name = "啦啦啦" });});PrintExecutionTime(() =>{Console.WriteLine("通過(guò)泛型類(lèi)緩存,第二次消耗時(shí)間:");PeopleCopy copy4 = ExpressionGenericMapper<People, PeopleCopy>.Trans(new People() { Age = 11, Name = "啦啦啦" });});

5、表達(dá)式目錄樹(shù)動(dòng)態(tài)生成的用途:

可以用來(lái)替代反射,因?yàn)榉瓷淇梢酝ㄓ?#xff0c;但是性能不夠

可以生成硬編碼,可以提升性能

6、遞歸解析表達(dá)式目錄樹(shù)

(1)ExpressionVisitor:肯定得遞歸解析表達(dá)式目錄樹(shù),因?yàn)椴恢郎疃鹊囊豢脴?shù)

(2)只有一個(gè)入口叫Visit

(3)首先檢查是個(gè)什么類(lèi)型的表達(dá)式,然后調(diào)用對(duì)應(yīng)的protected virtual

(4)得到結(jié)果繼續(xù)去檢查類(lèi)型——調(diào)用對(duì)應(yīng)的Visit方法——再檢測(cè)——再調(diào)用。。。

案例:解析(m*n+2)

第一步(把m*n+2傳入,入口時(shí)Visit)

第二步:檢測(cè)到二元表達(dá)式,m*n+2進(jìn)入VisitBinary方法.

node.Left為m*n(這里會(huì)再次調(diào)用VisitBinary方法)? ? node.Right為2

??第三步:m*n也時(shí)二元表達(dá)式,因此重新進(jìn)入VisitBinary方法

node.Left為m(進(jìn)入VisitParameter方法)? node.Right為n(進(jìn)入VisitParameter方法)

第四步:m*n解析完開(kāi)始解析2,會(huì)進(jìn)入VisitConstant方法

基礎(chǔ)應(yīng)用:我們可以把表達(dá)式的所有+號(hào)變成-號(hào)

從下圖可以發(fā)現(xiàn),表達(dá)式expression變成了m*n-2

7、表達(dá)式拼接

using System.Linq.Expressions;namespace ConsoleApp1
{public static class ExpressionExtend{public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T,bool>> expr2) {//return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, expr2.Body));ParameterExpression newParameter = Expression.Parameter(typeof(T), "c");NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter) ;var left = visitor.Replace(expr1.Body);var right = visitor.Replace(expr2.Body);var body = Expression.And(left, right);return Expression.Lambda<Func<T, bool>>(body, newParameter);}public static Expression<Func<T, bool>> Or<T(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) {ParameterExpression newParameter = Expression.Parameter(typeof(T), "c");NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter);var left = visitor.Replace(expr1.Body);var right = visitor.Replace(expr2.Body);var body = Expression.Or(left, right);return Expression.Lambda<Func<T, bool>>(body, newParameter);}public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, int>> expr) {var candidateExpr = expr.Parameters[0];var body = Expression.Not(expr.Body);return Expression.Lambda<Func<T, bool>>(body, candidateExpr);}}public class NewExpressionVisitor : ExpressionVisitor{public ParameterExpression _NewParameter { get; private set; }public NewExpressionVisitor(ParameterExpression param){this._NewParameter = param;}public Expression Replace(Expression exp){return this.Visit(exp);}protected override Expression VisitParameter(ParameterExpression node){return this._NewParameter;}}
}
       public static void Show(){Expression<Func<People, bool>> lambdal = x => x.Age > 5;Expression<Func<People, bool>> lambda2 = x => x.ID > 5;Expression<Func<People, bool>> lambda3 = lambdal.And(lambda2);Expression<Func<People, bool>> lambda4 = lambdal.Or(lambda2);Expression<Func<People, bool>> lambda5 = lambdal.Not();var peopleList = Do(lambda3);for (int i = 0; i < peopleList.Count; i++){Console.WriteLine(peopleList[i].Name);}}private static List<People> Do(Expression<Func<People, bool>> func){List<People> people = new List<People>(){new People() { ID = 1, Age = 10, Name = "老一" },new People() { ID = 2, Age = 20, Name = "老二" },new People() { ID = 3, Age = 60, Name = "老三" },new People() { ID = 4, Age = 18, Name = "老四" },new People() { ID = 5, Age = 10, Name = "哈哈哈" },new People() { ID = 6, Age = 15, Name = "方式" },new People() { ID = 7, Age = 10, Name = "老六啦啦啦啦啦啦啦" },};return people.Where(func.Compile()).ToList();}

調(diào)試成功,ID>5且age>5的只有這兩個(gè)人。

表達(dá)式樹(shù)也可以通過(guò)這種辦法進(jìn)行查詢(xún)操作

http://www.risenshineclean.com/news/5070.html

相關(guān)文章:

  • 無(wú)錫網(wǎng)站建設(shè)企業(yè)排名無(wú)錫營(yíng)銷(xiāo)型網(wǎng)站制作
  • 制作網(wǎng)站的策劃方案網(wǎng)站自建
  • 17網(wǎng)站一起做網(wǎng)店不發(fā)貨網(wǎng)上售賣(mài)平臺(tái)有哪些
  • 做短視頻網(wǎng)站產(chǎn)品營(yíng)銷(xiāo)方案案例范文
  • 網(wǎng)站怎么做谷歌權(quán)重色盲眼鏡
  • 武漢模板建站平臺(tái)哪家好鏈接提交工具
  • 杭州網(wǎng)站制如何在其他平臺(tái)做推廣
  • 濟(jì)南建站免費(fèi)模板瀏覽器網(wǎng)站進(jìn)入口
  • 北京出名做網(wǎng)站的公司如何做好推廣
  • 阿里云認(rèn)證網(wǎng)站建設(shè)題庫(kù)免費(fèi)制作網(wǎng)頁(yè)平臺(tái)
  • 企業(yè)做網(wǎng)站設(shè)計(jì)的北京網(wǎng)絡(luò)推廣外包公司排行
  • 成都市城鄉(xiāng)建設(shè)委員網(wǎng)站seo網(wǎng)站診斷價(jià)格
  • 要給公司做一個(gè)網(wǎng)站怎么做網(wǎng)站優(yōu)化的方式有哪些
  • 北京比較好的網(wǎng)站建設(shè)公司谷歌seo服務(wù)
  • 做競(jìng)價(jià)網(wǎng)站用什么系統(tǒng)好原創(chuàng)軟文
  • dede推薦評(píng)級(jí)網(wǎng)站模版百度搜索推廣費(fèi)用
  • 免費(fèi)企業(yè)網(wǎng)站空間網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃的具體流程是
  • 越南做網(wǎng)站服務(wù)器最新app推廣項(xiàng)目平臺(tái)
  • 網(wǎng)站公司建站營(yíng)銷(xiāo)活動(dòng)
  • 哪個(gè)網(wǎng)站可以做視頻播放器最新全國(guó)疫情消息
  • 西安有哪些做網(wǎng)站建設(shè)的公司好網(wǎng)絡(luò)推廣計(jì)劃書(shū)范文
  • 游戲推廣是做什么的關(guān)鍵詞優(yōu)化公司排名
  • html5響應(yīng)式網(wǎng)站制作seo網(wǎng)站排名廠(chǎng)商定制
  • 成都 網(wǎng)站設(shè)計(jì)網(wǎng)站建設(shè)工作總結(jié)
  • 黔東南網(wǎng)站開(kāi)發(fā)gzklyy手機(jī)網(wǎng)站建設(shè)價(jià)格
  • 沒(méi)有基礎(chǔ)怎么學(xué)網(wǎng)站建設(shè)seo短視頻網(wǎng)頁(yè)入口引流免費(fèi)
  • 寧波做網(wǎng)站的大公司seo網(wǎng)站優(yōu)化師
  • 加猛掙錢(qián)免費(fèi)做網(wǎng)站軟件免費(fèi)網(wǎng)站推廣網(wǎng)站破解版
  • 自己做的網(wǎng)站服務(wù)器開(kāi)了進(jìn)不去百度號(hào)碼認(rèn)證平臺(tái)官網(wǎng)
  • 微信公眾平臺(tái)制作網(wǎng)站58網(wǎng)絡(luò)推廣