English 中文(简体)
如何利用一名代表的名字来获得一个被汇编成册的人。
原标题:how to use a delegate to get by name a CompiledQuery

我试图找到并经营一个名叫Dompiled Query的人。 我如何以姓名查阅汇编的问题,然后我如何援引代表?

这里,我可以找到——我发现错误,对目标方法具有约束力。

public class ActivityRepository
{
    private readonly ActivityDataContext _db;

    public ActivityRepository()
    {
        _db = new ActivityDataContext();
    }

    public static Func<ActivityDataContext, int, IQueryable<ProjectObject>>
        GetCompiledLatestProjects = CompiledQuery.Compile
            ((ActivityDataContext db, int projectId) =>
             from c in db.projectObjects
             where c.projectId == projectId
             select c);

    public static Func<ActivityDataContext, Guid, IQueryable<Report>>
        GetCompiledReports = CompiledQuery.Compile
            ((ActivityDataContext db, Guid itemId) =>
             from c in db.Reports
             where c.reportObjectId == itemId
             select c);

页: 1 其他已编辑的查询,但结果是可贵的物体,可采用共同接口一项目。 目的

    delegate IQueryable<IProjectObject> MyDelegate();

    static MyDelegate GetByName(object target, string methodName)
    {
            return (MyDelegate)Delegate.CreateDelegate
                (typeof(MyDelegate), target, methodName);
    }

    public IList<Results> GetResults(string reportName)
    {
            IQueryable<ProjectObject> projectItems = GetLatestProjectObjects(projectId, quantity);
        foreach (projectObject o in projectItems)
        {
            MyDelegate del = GetByName(this, reportName);
             var dbReport = (IProjectObject) GetCompiledReports(_db, o.itemId).FirstOrDefault();
// add results to List and return
         }
     }
}
最佳回答

也许,你可以采取一些漫长的节奏,努力查明任何类型的富克和富尔;......和;数据目录是归还可贵的第一份论点,但你可能发现,仅仅增加一个习俗特性会非常容易:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class DynamicQueryAttribute : Attribute { }

......你可以补充你的领域:

[DynamicQuery]
public static Func<ActivityDataContext, int, IQueryable<ProjectObject>>
    GetCompiledLatestProjects = CompiledQuery.Compile
        ((ActivityDataContext db, int projectId) =>
         from c in db.projectObjects
         where c.projectId == projectId
         select c);

之后,你可以选择基于此点的询问及其名称:

public static IQueryable<IProjectObject> ExecuteQuery(Type ownerType, string name, params object[] args)
{
    var query = typeof(ownerType)
        .GetFields(BindingFlags.Public | BindingFlags.Static)
        .Where(f =>
                (f.GetCustomAttributes(typeof(DynamicQueryAttribute), false).Length > 0)
                && (f.Name.ToLowerInvariant() == name.ToLowerInvariant()))
        .Select(f => (Delegate) f.GetValue(null))
        .SingleOrDefault();

    if (query == null)
        return null;

    return (IQueryable<IReportObject>)query.DynamicInvoke(args);
}

使用:

var results = ExecuteQuery(
    typeof(ActivityRepository),
    "GetCompiledLatestProjects", 
    dataContext, 
    projectId);

希望:

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签