你能告诉我以下代码有什么问题吗?
Panel div = new Panel();
Button btn1 = new Button { Text = "Delete", CommandArgument = "argument", ID = "remove" };
Button btn2 = new Button { Text = "Insert", CommandArgument = "argument2", ID = "insert" };
btn1.Click += new EventHandler(btn_click);
btn2.Click += new EventHandler(btn_click);
div.Controls.Add(btn1);
div.Controls.Add(btn2);
ph_plan.Controls.Add(div); // where ph_plan is a placeholder in the user control
protected void btn_click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if(btn.ID == "remove")
// do this
else
// do that …Run Code Online (Sandbox Code Playgroud) 我怎样才能实现类似下面的例子呢?
public interface IGenericRepository {
int id { get; }
T GetById<T>() where T : class
}
public class GenericRepository : IGenericRepository {
//Some code here
public T GetById<T>(int tid) where T : class
{
return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
}
}
Run Code Online (Sandbox Code Playgroud)
我想用这个如下:
GenericRepository gr = new GenericRepository();
Category cat = gr.GetById<Category>(15);
Run Code Online (Sandbox Code Playgroud)
当然这个代码不起作用,因为如果我定义T : class那么该tbl.id部分将无法工作.但当然,应该有一种方法来实现这一点.
UPDATE
考虑到driis的回答,我正在做以下事情,但我仍然无法做到这一点:
public interface IEntity
{
int id { get; }
}
public interface IGenericRepository …Run Code Online (Sandbox Code Playgroud) 考虑以下示例以更好地理解我的问题:
public class ClassName
{
public ClassName { }
public string Val { get; set; }
...
}
ClassName cn = new ClassName();
cn.Val = "Hi StackOverflow!!";
Run Code Online (Sandbox Code Playgroud)
python中这段代码的等价物是什么?
s = re.sub(r"<style.*?</style>", "", s)
Run Code Online (Sandbox Code Playgroud)
这段代码不应该删除s字符串中的样式吗?为什么不起作用?我想删除以下代码:
<style type="text/css">
body { ... }
</style>
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
那么,当线程启动时,这段代码如何退出while语句?(请不要考虑缩进)
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue, out_queue):
threading.Thread.__init__(self)
self.queue = queue
self.out_queue = out_queue
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
#grabs urls of hosts and then grabs chunk of webpage
url = urllib2.urlopen(host)
chunk = url.read()
#place chunk into out queue
self.out_queue.put(chunk)
#signals to queue job is done
self.queue.task_done()
Run Code Online (Sandbox Code Playgroud)
**编辑*
启动线程的代码:
def main():
#spawn a pool of threads, and pass them queue instance
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True) …Run Code Online (Sandbox Code Playgroud) 为什么以下代码在我添加了几个项后返回空?
class Con(list):
def __init__(self): pass
def __str__(self):
return ' '.join(self)
def add(self, value)
self.append(value)
i for i in range(10):
Con().add(i)
>>> print Con()
# empty space instead of:
0 1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
我还要为我的班级定义什么才能像列表一样行事?
我知道我可以手动显式设置和取消设置会话,但我相信这是值得问的.在c#中,有一个名为TempData的字典,它存储数据直到第一个请求.换句话说,当调用TempData时,它会自动取消设置.为了更好地理解这里是一个例子:
Controller1.cs:
TempData["data"] = "This is a stored data";
Run Code Online (Sandbox Code Playgroud)
Model1.cs:
string dst1 = TempData["data"]; // This is a stored data
string dst2 = TempData["data"]; // This string will be empty, if an exception is not raised (I can't remember well if an exception is raised)
Run Code Online (Sandbox Code Playgroud)
所以基本上,这只是一个仅供1次使用的会话.再一次,我知道我可以在php中明确设置和取消设置,但是,php是否有类似这样的功能?
x = "type='text'"
re.findall("([A-Za-z])='(.*?)')", x) # this will work like a charm and produce
# ['type', 'text']
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是我想实现一个管道(交替),以便相同的正则表达式适用
x = 'type="text"' # see the quotes
Run Code Online (Sandbox Code Playgroud)
基本上,以下正则表达式应该可以工作但是findall会导致一些奇怪的事情:
([A-Za-z])=('(.*?)')|"(.*?)")
Run Code Online (Sandbox Code Playgroud)
并且我不能使用['"]而不是管道,因为它可能以不良结果结束:
value="hey there what's up?"
Run Code Online (Sandbox Code Playgroud)
现在,我如何构建适用于单引号或双引号的正则表达式?顺便说一句,请不要建议任何html或xml解析器,因为我对它们不感兴趣.
c# ×5
python ×5
class ×2
regex ×2
asp.net ×1
asp.net-mvc ×1
border ×1
button ×1
click ×1
css ×1
css-tables ×1
dynamic ×1
equivalent ×1
findall ×1
generics ×1
html ×1
inheritance ×1
list ×1
methods ×1
nullable ×1
php ×1
pipe ×1
repository ×1
tempdata ×1
variables ×1
while-loop ×1