Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用<;创建用于保存类实例的变量;t来源>;使用c#?_C# - Fatal编程技术网

如何使用<;创建用于保存类实例的变量;t来源>;使用c#?

如何使用<;创建用于保存类实例的变量;t来源>;使用c#?,c#,C#,我有一个类,它需要传递一个对象,以便创建一个新的实例。我的班级看起来是这样的 public class TableMapper<TSource> { ........ } 我希望我可以这样调用我的关系方法 public override List<IReportRelation> ReportRelations { get { new List<IReportRe

我有一个类,它需要传递一个对象,以便创建一个新的实例。我的班级看起来是这样的

public class TableMapper<TSource>
{
   ........
}
我希望我可以这样调用我的
关系
方法

public override List<IReportRelation> ReportRelations
        {
            get
            {
                new List<IReportRelation>
                {
                    Mapper.Relation(x => x.ClientId, TableMapper<Client>, c => c.Id),
                };
            }
        }
公共覆盖列表报表关系
{
得到
{
新名单
{
关系(x=>x.ClientId,TableMapper,c=>c.Id),
};
}
}

我如何才能正确地创建一个属性,该属性包含另一个类的实例,而我每次传递给它的都是该类的实例?

将您的
IReportRelation
-接口更改为以下内容:

public class IReportRelation<TLocal, TForeign>
{
    public TableMapper<TLocal> localMapper { get; set; }

    public Func<string, string> localProperty { get; set; }

    public Func<string, string> foreignProperty { get; set; }

    public TableMapper<TForeign> foreignMapper { get; set; }
}

将您的
IReportRelation
-界面更改为以下内容:

public class IReportRelation<TLocal, TForeign>
{
    public TableMapper<TLocal> localMapper { get; set; }

    public Func<string, string> localProperty { get; set; }

    public Func<string, string> foreignProperty { get; set; }

    public TableMapper<TForeign> foreignMapper { get; set; }
}

TLocal和TForeign在这里应该做什么?如果您想在接口中使用泛型,接口必须知道预期的类型。感谢您的解释。那么我的关系方法如何创建这个类呢?我尝试了类似的方法,但是我得到了很多错误
public-ireportrelationship关系(Expression-lProperty,TableMapper-fMapper,Expression-fProperty){return-new-ireportrelationship{localProperty=lProperty,foreignProperty=fProperty};}
非常感谢。你介意帮我理解一下
关系
的用途吗?此外,是否应从代码中删除
foreignMapper=fMapper
?类不会自动知道
TableMapper
的两个实例吗?@Jaylen做了另一个更新。如果关系应该同时知道两个映射器,则这取决于您对映射器的使用。有一件事我不明白。如果您使用的是EntityFramework,为什么需要这样的映射器?TLocal和TForeign在这里应该做什么?如果您想在接口中使用泛型,接口必须知道预期的类型。感谢您的解释。那么我的关系方法如何创建这个类呢?我尝试了类似的方法,但是我得到了很多错误
public-ireportrelationship关系(Expression-lProperty,TableMapper-fMapper,Expression-fProperty){return-new-ireportrelationship{localProperty=lProperty,foreignProperty=fProperty};}
非常感谢。你介意帮我理解一下
关系
的用途吗?此外,是否应从代码中删除
foreignMapper=fMapper
?类不会自动知道
TableMapper
的两个实例吗?@Jaylen做了另一个更新。如果关系应该同时知道两个映射器,则这取决于您对映射器的使用。有一件事我不明白。如果您使用的是EntityFramework,为什么需要这样的映射器?
public override List<IReportRelation> ReportRelations
        {
            get
            {
                new List<IReportRelation>
                {
                    Mapper.Relation(x => x.ClientId, TableMapper<Client>, c => c.Id),
                };
            }
        }
public class IReportRelation<TLocal, TForeign>
{
    public TableMapper<TLocal> localMapper { get; set; }

    public Func<string, string> localProperty { get; set; }

    public Func<string, string> foreignProperty { get; set; }

    public TableMapper<TForeign> foreignMapper { get; set; }
}
public IReportRelation<T1, T2> Relation<T1, T2>(TableMapper<T1> lMapper, Func<string, string> lProperty, TableMapper<T2> fMapper, Func<string, string> fKey)
    {
        return new IReportRelation<T1, T2> { localMapper = lMapper, localProperty = lProperty, foreignMapper = fMapper, foreignProperty = fKey };
    }
public void SomeMethod(){                
            var xx = this.Relation(new TableMapper<Student>(), dummy, new TableMapper<Department>(), dummy);
        }

        string dummy(string xx)
        {
            return xx + "Hello";
        }