小编Jua*_*mez的帖子

异步表达式C#

我正在尝试使用某些方法生成一个存储库,并为这些方法包含Async版本,如下所示:

    //Method 1
    public static List<MyEntity> GetMyEntityByDate(MyContextType context)
    {
        var tmpMov = context.MyEntity
            .AsNoTracking()
            .ToList();

        return tmpMov; 

    }

    //Method 2 V1
    public async static List<MyEntity> GetMyEntityByDateAsync(MyContextType context)
    {
        var tmpMov = await context.MyEntity
            .AsNoTracking()
            .ToListAsync();

        return tmpMov; 
    }

    //Method 2 V2
    public async static List<MyEntity> GetMyEntityByDateAsync(MyContextType context)
    {
        List<MyEntity> tmpMov = null;  
        await Task.Factory.StartNew(() => { 
            tmpMov = GetMyEntityByDate(context);
        });

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

现在我有一些问题:

在性能和流畅性方面,使用Method 2 V1和Method2 V2有什么优缺点?

在有人关注这个问题之前,是的,我正在实现一个存储库模式,并希望编写更少的代码,这就是我考虑V2的原因,这样我只需要维护一个查询.

但是我在异步上的经验很差,而且我的目标是获得最好的表现.

c#

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

我的类被实例化时需要运行受保护的方法

我有一个带有一些自定义属性的基类,这个类是许多其他clases的基类.这是我的类:

[CustomAttribute1("First")]
[CustomAttribute2("Other")]
public class Foo
{
    protected void checkAttributes()
    {
        // Need to run this code when the class and derived classes are instantiated 
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,如果我打造一个实例

Foo MyClass = new Foo();
Run Code Online (Sandbox Code Playgroud)

或者建立一个失败的类:

[CustomAttribute1("FirstDerived")]
public CustClass : Foo
{
    public CustClass(int MyVar)
    {
        //Something here
    }
    public void OtherMethod()
    {
    }
}

CustClass MyClass = new CustClass(5);
Run Code Online (Sandbox Code Playgroud)

我需要方法checkAttributes()总是运行.

是Posible吗?

还有另一种方法吗?

注意:我需要shure checkAttributes()运行,即使在派生类中重构了构造函数:

c#

0
推荐指数
1
解决办法
161
查看次数

如何将字符串转换为int

我有一组名为这样的对象:

_1,_2,_3 .... _n
Run Code Online (Sandbox Code Playgroud)

不是一个数组,只是一组自定义控件的对象.我想将名称转换为int.首先我删除_,但如果使用

    private void Superficie_MouseDown(object sender, MouseButtonEventArgs e)
    {
        int index = 0;
        Shape myShape = ((Shape)sender);
        string lcNombre = myShape.Name.Remove(0, 1);

        //--------------------------------------------------------------------
        // Those are my tryes  
        // Doesn't work because it must expect a nullable value 
        index = lcNombre as decimal; 

        // Doesn't work  Can't convert string to int
        index = (int)lcNombre;
        //--------------------------------------------------------------------


        if (index > 0)
        {
            bool lIsSelected =  !Pieza.Superficies.Where(x=>x.Id == index).First().IsSelected;
            Pieza.Superficies.Where(x => x.Id == index).First().IsSelected = lIsSelected;

            if (lIsSelected) …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
451
查看次数

WPF样式自定义控件

我有一个继承自的类 TextBox

public class DecimalTextBox : TextBox
{

    #region Float Color 
    public static readonly DependencyProperty FloatColorProperty = DependencyProperty.Register("FloatColor", typeof(Color), typeof(DecimalTextBox), new FrameworkPropertyMetadata(Colors.Red));
    public Color FloatColor
    {
        get { return (Color)GetValue(FloatColorProperty); }
        set { SetValue(FloatColorProperty, value); }
    }
    #endregion

    . . . . ..  OTHER STUFF
}
Run Code Online (Sandbox Code Playgroud)

我想用这样的东西来设置这个控件的样式:

<Style x:Key="DecimalTextBoxGridStyle" TargetType="DecimalTextBox">
    <Setter Property="TextAlignment" Value="Right"/>
    <Setter Property="FloatColor" Value="Black"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

但它的风格告诉我

在此输入图像描述

在wpf项目中不会混淆DecimalTexbox类型

我怎么能这样做?

还有另一种方法吗?

c# wpf

0
推荐指数
1
解决办法
51
查看次数

标签 统计

c# ×4

wpf ×1