小编Pro*_*ool的帖子

jQuery:暂时更改样式然后重置为原始类

假设我有一个名为testThing的类:

.testThing
{
   background-color:#000000;
   float:left;
   height:50px;
   width:50px;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在单击按钮时测试背景颜色更改为该类的任何控件:

function setColor(someColor) 
{
   jQuery('.testThing').css('background-color', someColor);
}
Run Code Online (Sandbox Code Playgroud)

但我希望用户能够根据类的内容重置为原始颜色(另一个按钮单击):

function resetClass()
{
   jQuery('#currentColor').removeClass('testThing');
   jQuery('#currentColor').addClass('testThing');
}
Run Code Online (Sandbox Code Playgroud)

看起来这样可行(Albiet不是最好的方法)但控件的背景颜色不会重置为该类中保存的原始值.

现在要么我需要弄清楚为什么删除添加不会重置它或只是一个更好的方法来做...看到删除和读取类似乎很愚蠢...

css jquery

56
推荐指数
2
解决办法
9万
查看次数

C#Image.Clone内存异常

为什么我会出现内存不足异常?

所以这首次在C#中死掉了:

splitBitmaps.Add(neededImage.Clone(rectDimensions,neededImage.PixelFormat));

其中splitBitmaps是List <BitMap>但是这在VB中工作至少4次迭代:

arlSplitBitmaps.Add(Image.Clone(rectDimensions,Image.PixelFormat))

其中arlSplitBitmaps是一个简单的数组列表.(是的,我在c#中试过arraylist)

这是完整的部分:

for (Int32 splitIndex = 0; splitIndex <= numberOfResultingImages - 1; splitIndex++)
{ 
  Rectangle rectDimensions;

  if (splitIndex < numberOfResultingImages - 1) 
  {
    rectDimensions = new Rectangle(splitImageWidth * splitIndex, 0, 
      splitImageWidth, splitImageHeight); 
  } 
  else 
  {
    rectDimensions = new Rectangle(splitImageWidth * splitIndex, 0, 
     sourceImageWidth - (splitImageWidth * splitIndex), splitImageHeight); 
  } 

  splitBitmaps.Add(neededImage.Clone(rectDimensions, neededImage.PixelFormat)); 
Run Code Online (Sandbox Code Playgroud)

}

aptImage顺便说一句就是Bitmap.

我在intarweb上找不到任何有用的答案,尤其不是为什么它在VB中运行得很好.

更新:

我实际上找到了这个工作的原因(但有点),但忘了发布它.它与将图像转换为位图有关,而不仅仅是在我记得的情况下尝试克隆原始图像.

c# image exception

47
推荐指数
4
解决办法
4万
查看次数

动态参数使编译器认为方法返回是动态的

如果我有一个动态参数,编译器似乎抛弃了返回类型并认为它是动态的.

例如:

public MethodResult IsValid(object userLogin)
{     
  return new MethodResult();
}
Run Code Online (Sandbox Code Playgroud)

你会认为:

var isValidResult = IsValid(someObject());
Run Code Online (Sandbox Code Playgroud)

应该读作

dynamic -> MethodResult 
Run Code Online (Sandbox Code Playgroud)

但它认为它是:

dynamic -> dynamic
Run Code Online (Sandbox Code Playgroud)

为签名添加动态参数是否完全阻止编译器知道返回应该是什么,尽管返回是强类型的?

c# c#-4.0 dynamictype

31
推荐指数
1
解决办法
1097
查看次数

Javascript:如何让Control在方法中发送自己

<a href="" id="someId" onclick="SomeMethod(self);"></a>
Run Code Online (Sandbox Code Playgroud)

SomeMethod可能有的地方:

function SomeMethod(item)
{
  item.setAttribute('name', item.id);
}
Run Code Online (Sandbox Code Playgroud)

代替:

function SomeMethod(itemId)
{
  var someItem;

  someItem = document.getElementById(itemId);
  someItem .setAttribute('name', someItem .id);

}
Run Code Online (Sandbox Code Playgroud)

愚蠢的例子,但想法不是发送id本身,而是实际控制调用方法.我发誓这可以做,但没有运气搜索...部分是因为我甚至不确定要搜索什么.

我认为这是自我,但当我运行的剧本时,自我似乎不是我想要的.

javascript onclick

28
推荐指数
3
解决办法
4万
查看次数

实体框架:LINQ to Entities仅支持转换实体数据模型基元类型

我编写了一个方法来允许为orderby子句传递Expression,但我遇到了这个问题.

无法将类型"System.DateTime"强制转换为"System.IComparable"类型.LINQ to Entities仅支持转换实体数据模型基元类型.

基本上表达式如下:

Expression<Func<K, IComparable>> orderBy
Run Code Online (Sandbox Code Playgroud)

并使用如下:

SomeEntities.SomeTable
.Where
(
   whereClause
)
.Select
(
   selectClause
)
.OrderBy(orderBy)
Run Code Online (Sandbox Code Playgroud)

我的想法是,我可以使用字典来保存字符串匹配,例如:

_possibleSortForForumItem.Add("CreateDate", item => item.CreateDate);
Run Code Online (Sandbox Code Playgroud)

然后我有一个方法接受排序字符串并返回表达式,如果它匹配字典中的键,如果不返回一些默认值.(这个想法是一种控制它可以被命令的方式)现在这适用于String属性,但到目前为止还没有为datetime或整数,因为我得到上面的错误消息.

到目前为止,我(松散地)理解问题是实体框架需要它是主/ EDM类型,因为它必须将C#DateTime转换为数据库可以处理的内容.

有没有办法将日期时间转换为基本类型,以便这仍然有效?

通过方法获取订单的方法:(接受查询并以"有序形式"返回)

private static Func<IQueryable<ForumViewItem>, IOrderedQueryable<ForumViewItem>> GetMethodForSort(String sortBy)
{
  if (_methodForSort == null)
  {
    _methodForSort = new Dictionary<String, Func<IQueryable<ForumViewItem>, IOrderedQueryable<ForumViewItem>>>();
    _methodForSort.Add(SortForumViewItemCreatedOn, item => item.OrderBy(innerItem => innerItem.CreatedOn));
    ...
  }

  Func<IQueryable<ForumViewItem>, IOrderedQueryable<ForumViewItem>> orderMethod;

  if(String.IsNullOrEmpty(sortBy) || !_methodForSort.ContainsKey(sortBy))
  {
    orderMethod = _methodForSort["ForumName"];
  }
  else
  {
    orderMethod = _methodForSort[sortBy];
  }

  return orderMethod;
}
Run Code Online (Sandbox Code Playgroud)

通用查询方法的方法签名:

IList<K> GetListForGrid<T, K>(this …
Run Code Online (Sandbox Code Playgroud)

entity-framework primitive-types

24
推荐指数
3
解决办法
2万
查看次数

有没有更好的方法来编写"字符串包含X"方法?

只是盯着使用Haskell并实现(据我所知),没有直接的方法来检查字符串,看它是否包含一个较小的字符串.所以我想我只是试一试.

基本上,我们的想法是检查两个字符串是否大小相同并且相等.如果检查的字符串较长,则递归地删除头部并再次运行检查,直到检查的字符串长度相同.

其余的可能性我使用模式匹配来处理它们.这就是我想出的:

stringExists "" wordToCheckAgainst = False
stringExists wordToCheckFor "" = False
stringExists wordToCheckFor wordToCheckAgainst | length wordToCheckAgainst < length wordToCheckFor = False
                                               | length wordToCheckAgainst == length wordToCheckFor = wordToCheckAgainst == wordToCheckFor
                                               | take (length wordToCheckFor) wordToCheckAgainst == wordToCheckFor = True
                                               | otherwise = stringExists wordToCheckFor (tail wordToCheckAgainst)
Run Code Online (Sandbox Code Playgroud)

recursion haskell pattern-matching

21
推荐指数
3
解决办法
2万
查看次数

实体框架:使用事务和回滚......可能吗?

问题:(使用Sql 2005)

  • 如何在事务处理时查询数据库?(因为它锁定表)
  • 如何导致事务回滚然后关闭自己以允许查询表?

所以我发现了这么多:

[TestMethod]
public void CreateUser()
{
    TransactionScope transactionScope = new TransactionScope();

    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();

    DataContextHandler.Context.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

DataContextHandler只是一个简单的单例,它为我的实体公开了上下文对象.这似乎就像你想象的那样有效.它创建用户,保存,然后在程序结束时回滚.(IE测试结束)

问题:如何强制事务回滚并自行终止以便我可以查询表?

原因:出于测试目的,我想确保用户:

  • 已保存
  • 可以正确查询以证明其存在
  • 被删除(垃圾数据)
  • 可以查询以确保它已被删除.

目前,如果测试结束,我只能让事务回滚,而我无法弄清楚如何查询事务:

[TestMethod]
public void CreateUser()
{
    ForumUser userToTest = new ForumUser();

    TransactionScope transactionScope = new TransactionScope();

    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();     

    Assert.IsTrue(userToTest.UserID > 0);

    var foundUser = (from user in DataContextHandler.Context.ForumUser
                    where user.UserID == userToTest.UserID
                    select user).Count();  //KABOOM Can't query since the 
                                           //transaction has the table locked.

    Assert.IsTrue(foundUser == 1);

    DataContextHandler.Context.Dispose();

    var after = (from user in …
Run Code Online (Sandbox Code Playgroud)

entity-framework transactions

18
推荐指数
3
解决办法
5万
查看次数

C#:TypeDescriptor.GetAttributes()和GetType().GetCustomAttributes有什么区别?

拿这两个代码:

instance.GetType()
 .GetCustomAttributes(true)
 .Where(item => item is ValidationAttribute);
Run Code Online (Sandbox Code Playgroud)

TypeDescriptor.GetAttributes(instance)
 .OfType<ValidationAttribute>();
Run Code Online (Sandbox Code Playgroud)

如果这个类看起来像:

[RequiredIfOtherPropertyIsNotEmpty("State", "City", ErrorMessage = ErrorDescription.CreateAccount_CityRequiredWithState)]
[RequiredIfOtherPropertyIsNotEmpty("State", "Address1", ErrorMessage = ErrorDescription.CreateAccount_Address1RequiredWithState)]
public class ManagePostModel
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

RequiredIfOtherPropertyIsNotEmptya ValidationAttribute和a 在哪里AllowMultiple = true.

第一个返回两个属性,第二个返回一个.

导致这种情况的区别是什么?

c# attributes typedescriptor

13
推荐指数
1
解决办法
3544
查看次数

实体框架:Singletonish ObjectContext - Good,Bad还是Overthinking?

我们的想法是创建一个暴露上下文但在Web应用程序中处理它的存储的类.

目前这就是我所拥有的:

public class EntityContext
{

    private static String MAIN_CONTEXT_KEY = "MainContext";
    private static TISQLEntities _context;

    public static void RemoveContext()
    {
        if (
            HttpContext.Current != null 
            && 
            HttpContext.Current.Items[MAIN_CONTEXT_KEY] != null
           )
        {
            ((TISQLEntities)HttpContext.Current.Items[MAIN_CONTEXT_KEY]).Dispose();
            HttpContext.Current.Items[MAIN_CONTEXT_KEY] = null;
        }

        if (_context != null)
        {
            _context.Dispose();
            _context = null;
        }
    }

    public static TISQLEntities Context
    {
        get
        {
            if (HttpContext.Current == null)
            {
                if (_context == null)
                {
                    _context = new TISQLEntities();
                }

                return _context;
            }

            if (HttpContext.Current.Items[MAIN_CONTEXT_KEY] == null)
            {
                HttpContext.Current.Items[MAIN_CONTEXT_KEY] = …
Run Code Online (Sandbox Code Playgroud)

asp.net entity-framework

9
推荐指数
1
解决办法
6160
查看次数

使用"如布尔?" 而不是"对象的东西= ViewState ["hi"]"

所以我要通过旧代码(2.0),我遇到了这个:

object isReviewingValue = ViewState["IsReviewing"];

if (isReviewingValue is bool)
{
  return (bool)isReviewingValue;
}
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是给我们"as"关键字,以避免不必要的

(bool)isReviewingValue;
Run Code Online (Sandbox Code Playgroud)

但"as"仅适用于非值类型.没问题,我就这样做了:

bool? isReviewingValue= ViewState["IsReviewing"] as bool?;
if (isReviewingValue.HasValue)
{
  return isReviewingValue.Value;
}
Run Code Online (Sandbox Code Playgroud)

问题是:除了看起来更具可读性之外,这实际上更好吗?

编辑:

public Stopwatch AsRun()
{
  Stopwatch watch = new Stopwatch();

  watch.Start();
  for (Int32 loopCounter = 0; loopCounter < 10000; loopCounter++)
  {
    Object value = true;
    Boolean? test = value as Boolean?;
    if (test.HasValue)
    {
      Boolean something = test.Value;
    }
  }
  watch.Stop();

  return watch;
}

public Stopwatch ObjectIsRun()
{
  Stopwatch watch = new …
Run Code Online (Sandbox Code Playgroud)

c#

8
推荐指数
2
解决办法
5909
查看次数