在寫模版解析時碰到的一些問題,委託真好用

2021-05-02 22:14:33 字數 2279 閱讀 2231

模版標籤主要分為兩大類,欄目相關標籤和文章相關標籤,由於分屬於兩個表,對應不同的實體類,在替換模版標籤的時候就出現了些問題.先把枋心類貼出來,好說明問題.

stringpatter = @"([/s/s]*?)"

; matchcollection maction = regex.matches(tempcontext, patter, regexoptions.compiled); 

foreach(match macinmaction)  } 

stringtemp = tagtable == "channel"

?

在根據表名迴圈替換標籤的時候,第一反應使用泛型方法:

attribute方法兩個版本:

(1)attribute(string pattern,string loopstr,marticle mart)

(2)attribute(string pattern,string loopstr,mchannel mchannel)

private

stringloop(stringpatter,stringloopstr,listobj)  } 

還沒等高興過來,就發現編譯報錯,"t"型別不具有marticle,mchannel兩個類的屬性,nnd,泛型也不是萬能;

接下來又嘗試第二種方案:

把這兩個方法分離到兩個單獨的類中,在根據條件來例項化,功能是實現了,可**怎麼看怎麼便扭,兩個類的**百分之七八十是重複的,不爽,放棄.

這時想到了委託:

delegate

stringloopreplace(listobj); 

loopreplacelooparticle =delegate(listmarts) 

returntemps; 

}; loopreplaceloopchannel =delegate(listmarts) 

returntemps; 

};stringtemp = tagtable == "channel"

? loopchannel(channel.tempsql(strsql.tostring())) : looparticle(articles.tempsql(strsql.tostring())); 

比起第二方案來,大碼大大減少,可還是感覺不爽,為啥就不能像泛型那樣乙個方法就足以應付各種情況呢?再次改良:

delegate

stringreplace(stringpattern,stringloop,t obj); 

private

stringloop(stringpattern,stringloop,listobj,replacemethod) 

returntemps; 

}stringtemp = tagtable == "channel"

? loop(@"/["

+ labname + @":(.+?)/]"

, loopstr, channel.tempsql(strsql.tostring()), attribute) : loop(@"/["

+ labname + @":(.+?)/]"

, loopstr, articles.tempsql(strsql.tostring()), attribute);

這次總算是讓自己滿意了 :) 以前很少用委託,現在才發現它是這麼可愛!!!
http://www.railes.cn/zhan/547.html

mysql碰到的一些問題

今天碰到了一些mysql問題總結一下 error 1100 hy000 table tablename was not locked with lock tables 出現這種問題的原因很多,今天的問題是連線達到了上線,以前設計是10w左右,超過了此資料,就報了上面的錯誤,更改連線數就可以了。有關於...

自己碰到的一些問題

把平時工作中碰到的一些問題和解決辦法寫出來,共勉 1.kcafillmoderemoved,這個是預設值,也就是說當動畫開始前和結束後,動畫對layer都沒有影響,動畫結束後,layer會恢復到原來的狀態 2.kcafillmodeforwards,當動畫結束後,layer不會恢復到原來的狀態,會一...

Mongo db碰到的一些問題

mongo 的總結 1.許可權不夠error couldn t add user command createuser requires authentication 解決方案 先使用超級管理員登入,再進行其他資料庫使用者建立的建立。先退出 命令列,然後重新進入認證,只認證乙個使用者,不要多次使用d...