Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
C# 使用动态类型作为它的参数';s自己的基类型_C#_.net_F#_Reflection.emit - Fatal编程技术网

C# 使用动态类型作为它的参数';s自己的基类型

C# 使用动态类型作为它的参数';s自己的基类型,c#,.net,f#,reflection.emit,C#,.net,F#,Reflection.emit,我试图编写一些代码,在运行时生成一个类型。我需要实现一个接口,但是这个限制给我带来了一些困难 正如注释中所指出的,是的,接口看起来像无休止的递归,但实际上不是。它编译得很好 界面看起来类似于: interface IFoo<T> where T : IFoo<T>{ T MyProperty{get;} } 我必须通过一个IFoo,其中T是我试图定义的类型吗 上面的代码用C#表示,但用F#表示,让我们动态构造类SomeType:IFoo也可以 使用基类型而不是接口

我试图编写一些代码,在运行时生成一个类型。我需要实现一个接口,但是这个限制给我带来了一些困难

正如注释中所指出的,是的,接口看起来像无休止的递归,但实际上不是。它编译得很好

界面看起来类似于:

interface IFoo<T> where T : IFoo<T>{
   T MyProperty{get;}
}
我必须通过一个IFoo,其中T是我试图定义的类型吗

上面的代码用C#表示,但用F#表示,让我们动态构造
类SomeType:IFoo
也可以

使用基类型而不是接口的答案也是有效的(如标题所示)。即

在哪里。。是
SomeType
,T是定义的类型

编辑: 例如,当用代码编写时,可以是:

    public interface ISelf<T> where T : ISelf<T>
    {
        T Prop { get; }
    }

    public class SelfBase<T> : ISelf<T> where T : SelfBase<T>{
        public T Prop  { get { return (T)this; } }
    }

    public class FooBar : SelfBase<FooBar>{
        public void Bar(){
            Prop.NonInterfaceMethod();
        }

        public void NonInterfaceMethod(){}
    }
公共接口ISelf,其中T:ISelf
{
T Prop{get;}
}
公共类SelfBase:ISelf,其中T:SelfBase{
公共T Prop{get{return(T)this;}
}
公共类FooBar:SelfBase{
公共空白栏(){
Prop.NonInterfaceMethod();
}
public void NonInterfaceMethod(){}
}

这段代码确实可以编译。

您只需在
TypeBuilder
上使用
SetParent
方法即可。下面是如何在F#中执行此操作:

开放系统
开放系统。反射
开放系统.Reflection.Emit

键入SelfBase SelfBase这是否有效?这个接口定义对我来说就像一个无休止的递归…你的接口看起来很奇怪。T是一个IFoo,这里T也是一个IFoo,依此类推。这看起来好像会产生某种溢出,因为您无法指定T。关键字:recursion@Daniel它确实有效。在尝试动态之前,我开始编译一个静态版本。我将其视为的一个版本。它确实是编译的。我会说,使用
ModuleBuilder
TypeBuilder
类是不可能的,因为它们的API需要一个
类型来实现接口,而只有在类型生成器上调用
CreateType
时,才有一个类型,此时为时已晚。。。
 TypeBuilder tb = mb.DefineType(
                "typename",
                 TypeAttributes.Public,...,null);
    public interface ISelf<T> where T : ISelf<T>
    {
        T Prop { get; }
    }

    public class SelfBase<T> : ISelf<T> where T : SelfBase<T>{
        public T Prop  { get { return (T)this; } }
    }

    public class FooBar : SelfBase<FooBar>{
        public void Bar(){
            Prop.NonInterfaceMethod();
        }

        public void NonInterfaceMethod(){}
    }
open System
open System.Reflection
open System.Reflection.Emit

type SelfBase<'t when 't :> SelfBase<'t>> =
    member x.Prop = x :?> 't

type Foo = class
    inherit SelfBase<Foo>
end

let ab = AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName("test"), AssemblyBuilderAccess.Run)
let mb = ab.DefineDynamicModule("test")
let tb = mb.DefineType("typename", TypeAttributes.Public)
tb.SetParent(typedefof<SelfBase<Foo>>.MakeGenericType(tb))
let ty = tb.CreateType()

// show that it works:
let instance = System.Activator.CreateInstance(ty)
let prop = instance.GetType().GetProperties().[0].GetValue(instance, null)
let same = (prop = instance)