【问题标题】:List anonymous type to List of interface [duplicate]将匿名类型列出到接口列表[重复]
【发布时间】:2012-11-09 12:13:49
【问题描述】:

可能重复:
Dynamically implementing an interface in .NET 4.0 (C#)
cast anonymous type to an interface?

有没有办法将匿名类型“转换”到特定接口?我知道我可以创建一个实现这个接口的类,但我不需要这个类,我必须返回一个接口。

我需要一个没有第三方库的解决方案

谢谢,

var result =
    (from a in context.TABLE1
     join b in context.TABLE2 on a.Table2Id equals b.Id
     select new
     {
         Table1Field1 = a.Field1,
         Table2Field1 = b.Field1,
         ....
     }).ToList<IMyClass>();

public interface IMyClass
{
    string Table1Field1 { get; set; }
    string Table1Field2 { get; set; }
    string Table2Field1 { get; set; }
}

【问题讨论】:

  • 我看到这个帖子但是使用了外部库,这里不允许。
  • 如果您阅读更多内容,您会看到 Jon Skeet 的回答说这是不可能的 :)

标签: c# .net


【解决方案1】:

这是不可能的。为什么?因为匿名类型是一种语法糖。匿名类型是一种设计时特性,这意味着编译器将生成一个名称非常奇怪的实际类型,但毕竟它与任何其他类型一样。

遗憾的是,C# 没有接口自动实现。也就是说,你需要实现一个named类型的接口。

更新

想要解决这个限制吗?

您可以使用控制反转(使用 Castle Windsor 之类的 API 或仅手动)。

查看我刚刚制作的这个示例代码:

public static class AnonymousExtensions
{
    public static T To<T>(this object anon)
    {
        // #1 Obtain the type implementing the whole interface
        Type implementation = Assembly.GetExecutingAssembly()
                                .GetTypes()
                                .SingleOrDefault(t => t.GetInterfaces().Contains(typeof(T)));

        // #2 Once you've the implementation type, you create an instance of it
        object implementationInstance = Activator.CreateInstance(implementation, false);

        // #3 Now's time to set the implementation properties based on
        // the anonyous type instance property values!
        foreach(PropertyInfo property in anon.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
        {
            // Has the implementation this anonymous type property?
            if(implementation.GetProperty(property.Name) != null)
            {
                // Setting the implementation instance property with the anonymous
                // type instance property's value!
                implementation.GetProperty(property.Name).SetValue(implementationInstance, property.GetValue(anon, null));
            }
        }

        return (T)implementationInstance;
    }
}

设计和实现一些接口...

// Some interface
public interface IHasText
{
    string Text { get; set; }
}

// An implementation for the interface
public class HasText : IHasText
{
    public string Text
    {
        get;
        set;
    }
}

现在在某处使用整个扩展方法!

var anonymous = new { Text = "Hello world!" };
IHasText hasTextImplementation = anonymous.To<IHasText>();

hasTextImplementation 将有一个 HasText 实现实例!或者换句话说:Text 属性将包含 Hello world!

请注意,可以调整此代码以支持基类和抽象类,但我相信这足以获取基本信息以根据需要改进它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多