【问题标题】:Create property in runtime and pass value在运行时创建属性并传递值
【发布时间】:2016-06-03 14:25:04
【问题描述】:

我找到了创建运行时函数的示例。

public static MethodInfo CreateFunction(string function)
    {
        string code = @"
        using System;

        namespace UserFunctions
        {                
            public class BinaryFunction
            {                
                public static double Function(double x, double y)
                {
                    return func_xy;
                }
            }
        }
            ";

        string finalCode = code.Replace("func_xy", function);

        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerResults results = provider.CompileAssemblyFromSource(new CompilerParameters(), finalCode);

        Type binaryFunction = results.CompiledAssembly.GetType("UserFunctions.BinaryFunction");
        return binaryFunction.GetMethod("Function");
    }

主要:

static void Main(string[] args)
{
    MethodInfo function = CreateFunction("x + 2 * y");
    object result = function.Invoke(null, new object[] { 2, 3 });
    Console.WriteLine(result);
}

主要问题是如何在运行时创建属性并将值传递给Main

public static PropertyInfo CreateProperty()
    {
        string code=@"
        private string name;

        public string Name {
            get{
                return this.name;
            }
            set{
                this.name=value;
            }
        }
        ";
        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerResults results = provider.CompileAssemblyFromSource(new CompilerParameters(),code);
    ///
    }

【问题讨论】:

    标签: c# reflection csharpcodeprovider


    【解决方案1】:

    同样的方式?

    public static Type CreateProperty(string value)
    {
        string code = @"
        namespace UserFunctions
        {                
                public class MyProperty
                {          
                private string name = my_value
    
                public string Name {
                    get{
                        return this.name;
                    }
                    set{
                        this.name=value;
                    }
                }
            }
        }
    ";
        code = code.Replace("my_value", '"' + value + '"');
        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerResults results = provider.CompileAssemblyFromSource(new CompilerParameters(), code);
        Type myProperty = results.CompiledAssembly.GetType("UserFunctions.MyProperty");
        return myProperty;
    }
    
    static void Main()
    {
    
        Type propertyType = CreateProperty("this is a value");
        var property = propertyType.GetProperty("Name");
        var instance = Activator.CreateInstance(propertyType);
    
        Console.WriteLine(property.GetValue(instance, null));
    
        property.SetValue(instance, "MyNewValue", null);            
        Console.WriteLine(property.GetValue(instance, null));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2011-09-26
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多