C# 我试图将两个类传递到一个方法中,但收到一条消息,上面说;没有新的()约束?

C# 我试图将两个类传递到一个方法中,但收到一条消息,上面说;没有新的()约束?,c#,C#,我有一个方法: private static Style<ShellContent> SetGlyph(string glyph) { return new Style<ShellContent>().Add(new Trigger(typeof(ShellContent)) { Property = ShellContent.IsCheckedProperty, Value

我有一个方法:

    private static Style<ShellContent> SetGlyph(string glyph)
    {
        return new Style<ShellContent>().Add(new Trigger(typeof(ShellContent))
        {
            Property = ShellContent.IsCheckedProperty,
            Value = true,
            Setters =
            {
                new Setter()
                {
                    Property = ShellContent.IconProperty,
                    Value = new RegularTabIcon() { Glyph = glyph }
                },
                new Setter()
                {
                    Property = ShellContent.IconProperty,
                    Value = new SolidTabIcon() { Glyph = glyph }
                }
            }
        });
    }
我尝试创建一个类似这样的泛型类:

    private static Style<ShellContent> SetGlyph<T1, T2>(string glyph)
    {
        return new Style<ShellContent>().Add(new Trigger(typeof(ShellContent))
        {
            Property = ShellContent.IsCheckedProperty,
            Value = true,
            Setters =
            {
                new Setter()
                {
                    Property = ShellContent.IconProperty,
                    Value = new T1() { Glyph = glyph }
                },
                new Setter()
                {
                    Property = ShellContent.IconProperty,
                    Value = new T2() { Glyph = glyph }
                }
            }
        });
    }
有人能告诉我我做错了什么吗

另外,我如何指定T1和T2的类型必须是
fontmagesource

您必须将
new()
约束添加到方法的constant中

试试这个:

private static Style<ShellContent> SetGlyph<T1, T2>(string glyph) where T1 : new() where T2 : new()
{
    return new Style<ShellContent>().Add(new Trigger(typeof(ShellContent))
    {
        Property = ShellContent.IsCheckedProperty,
        Value = true,
        Setters =
        {
            new Setter()
            {
                Property = ShellContent.IconProperty,
                Value = new T1() { Glyph = glyph }
            },
            new Setter()
            {
                Property = ShellContent.IconProperty,
                Value = new T2() { Glyph = glyph }
            }
        }
    });
}
注意:如果同时使用
new()
和类型约束(例如:
FontImageSource
),则
new()
约束必须位于其他约束的末尾。将
new()
约束与其他约束一起使用时,必须最后指定该约束


有关类型参数约束的更多详细信息,请访问以下链接:

I更新了我的答案。我确实回答了您的最后一个问题(我如何指定T1和T2必须是FontImageSource类型?)错误消息对我来说似乎非常清楚。请参阅副本。
does not have a new() constraint
private static Style<ShellContent> SetGlyph<T1, T2>(string glyph) where T1 : new() where T2 : new()
{
    return new Style<ShellContent>().Add(new Trigger(typeof(ShellContent))
    {
        Property = ShellContent.IsCheckedProperty,
        Value = true,
        Setters =
        {
            new Setter()
            {
                Property = ShellContent.IconProperty,
                Value = new T1() { Glyph = glyph }
            },
            new Setter()
            {
                Property = ShellContent.IconProperty,
                Value = new T2() { Glyph = glyph }
            }
        }
    });
}
private static Style<ShellContent> SetGlyph<T1, T2>(string glyph) where T1 : FontImageSource, new() where T2 : FontImageSource, new()
{
    return new Style<ShellContent>().Add(new Trigger(typeof(ShellContent))
    {
        Property = ShellContent.IsCheckedProperty,
        Value = true,
        Setters =
                {
                    new Setter()
                    {
                        Property = ShellContent.IconProperty,
                        Value = new T1() { Glyph = glyph }
                    },
                    new Setter()
                    {
                        Property = ShellContent.IconProperty,
                        Value = new T2() { Glyph = glyph }
                    }
                }
    });
}