帮助需要添加新的方法,属性到现有的类动态属性、方法、动态

2023-09-06 23:58:12 作者:月亮是我踹弯的

我不知道是否有可能实现这种在斑点网实施。下面是信息

I am not sure whether it is possible to achieve this kind of implementation in Dot Net. Below are the information

目前,我们在其上的COM +,ASP,XSL,XML技术完成的应用程序。它是一个多层次的体系结构的应用中,COM +充当BAL。执行的步骤为任何CRUD操作将使用一个单独的UI它使用XML来存储的信息来定义。 BAL读取XML并理解其被定义的执行步骤和执行中的DLL对应的方法。就像火花,我们有我们的自定义模式(使用XML),它决定了对象的属性基于此信息BAL构造查询和调用程序,以获取数据检索,检索等。

Currently we are on an application which is done in COM+, ASP, XSL, XML technologies. It is a multi tier architecture application in which COM+ acts as the BAL. The execution steps for any CRUD operation will be defined using a seperate UI which uses XML to store the information. BAL reads the XML and understands the execution steps which are defined and executes corresponding methods in DLL. Much like EDM we have our custom model (using XML) which determines which property of object is searchable, retrievable etc. Based on this information BAL constructs queries and calls procedures to get the data.

在当前的应​​用既BAL和DAL是大量可自定义的,而不做任何code修改。其中构建了基于:收到的数据的用户界面的XML格式的结果将被传递到presentation层。

In the current application both BAL and DAL are heavily customizable without doing any code change. the results will be transmitted to presentation layer in XML format which constructs the UI based on the data recieved.

现在我创建一个迁移项目,该项目涉及的员工信息。它也将跟随N个层架构,其中presentation层用BAL它连接到DAL返回数据通信。

Now I am creating a migration project which deals with employee information. It is also going to follow the N Tier architecture in which the presentation layer communicates with BAL which connects to DAL to return the Data.

这是问题所在,在我们现有的版本中,我们正在处理每一个信息的XML的天然形式(无converstion对象等),但在迁移项目,团队是在利用开发,每一个的面向对象的模型很感兴趣需要这是从BAL发送的信息将被转换到其相应类型的对象(例如employeeCollection,地址集合等)。

Here is the problem, In our existing version we are handling every information as XML in its native form (no converstion of object etc), but in the migration project, Team is really interested in utilizing the OOP model of development where every information which is sent from BAL need to be converted to objects of its respective types (example employeeCollection, Address Collection etc).

如果我们从BAL返回我们可具有包含这些节点作为属性的类,我们可以访问同一数据的静态数。但是,在我们的例子中我们BAL需要自定义返回的数据。我们如何处理其中的计算结果转换为对象presentation层的定制。

If we have the static number of data returned from BAL we can have a class which contains those nodes as properties and we can access the same. But in our case the data returned from our BAL need to be customized. How can we handle the customization in presentation layer which is converting the result to an Object.

下面是XML的一个例子返回

Below is an example of the XML returned

<employees>
    <employee>
        <firstName>Employee 1 First Name</firstName>
        <lastName>Employee 1 Last Name</lastName>
        <addresses>
            <address>
                <addressType>1</addressType>
                <StreetName>Street name1</StreetName>
                <RegionName>Region name</RegionName>
            <address>
            <address>
                <addressType>2</addressType>
                <StreetName>Street name2</StreetName>
                <RegionName>Region name</RegionName>
            <address>
            <address>
                <addressType>3</addressType>
                <StreetName>Street name3</StreetName>
                <RegionName>Region name</RegionName>
            <address>
        <addresses>
    </employee>
    <employee>
        <firstName>Employee 2 First Name</firstName>
        <lastName>Employee 2 Last Name</lastName>
        <addresses>
            <address>
                <addressType>1</addressType>
                <StreetName>Street name1</StreetName>
                <RegionName>Region name</RegionName>
            <address>
            <address>
                <addressType>2</addressType>
                <StreetName>Street name2</StreetName>
                <RegionName>Region name</RegionName>
            <address>
        <addresses>
    </employee>
</employees>

如果这是唯一的列的话,我可以写一个类,就像

If these are the only columns then i can write a class which is like

public class Address{
    public int AddressType {get;set;};
    public string StreetName {get;set;};
    public string RegionName {get;set;};
}

public class Employee{
    public string  FirstName {get; set;}
    public string  LastName {get; set;}
    public string  AddressCollection {get; set;}
}

public class EmployeeCollection : List<Employee>{
    public bool Add (Employee Data){
    ....
    }
}

public class AddressCollection : List<Address>{
    public bool Add (Address Data){
    ....
    }
}

这班将提供给客户和顾问的DLL文件。我们将不提供源$ C ​​$ c代表相同。

This class will be provided to customers and consultants as DLLs. We will not provide the source code for the same.

现在当顾问或客户不(加国处理和添加护照信息对象与员工对象的实例),他们必须能够访问这些类的特性,但没有源$ C ​​$ C,他们将不能够自定义做那些modifications.which使得应用无用。有没有什么办法acomplish这DOTNET的。

Now when the consultants or customers does customization(example adding country to address and adding passport information object with employee object) they must be able to access those properties in these classes, but without source code they will not be able to do those modifications.which makes the application useless. Is there is any way to acomplish this in DotNet.

我想用匿名类的,但是,有匿名类的问题是

I thought of using Anonymous classes but, the problem with Anonymous classes are

我们不能在它的方法。 我不知道我怎样才能符合集合中的对象(这将是inturn匿名类) 不知道数据网格/用户控件绑定等。

we can not have methods in it. I am not sure how can i fit the collection objects (which will be inturn an anonymous class) Not sure about datagrid / user control binding etc.

我也想用codeDOM创建类运行,但不能确定meory,性能问题。也是类必须创建只有一次,必须使用相同的,直到有另一种变化。

I also thought of using CODEDom to create classes runtime but not sure about the meory, performance issues. also the classes must be created only once and must use the same till there is another change.

请帮我出这个问题。任何一种帮助材质的/神秘code /链接会有所帮助。

Kindly help me out in this problem. Any kind of help meterial/ cryptic code/ links will be helpful.

感谢这给了我寻找到我没有想到的不同领域所提供的答案。

Thanks for the answers that are provided which gave me to look into different areas which i had not thought of.

还有就是信息我错过了问题一peice的。用户界面code也将因为DLL文件不能作为源$ C ​​$ C。顾客可以自由地创建新的code和连接到用户界面,但他们不能更改通过code现有的用户界面。我们正处于一个设计师,这将显示可选择,可显示性能从中客户可以选择要显示的数据用户。这样,他们可以改变UI的外观和感觉。还他们可以使用该模型更改绑定属性

There is one peice of info i missed in the question. The UI code also will be as DLLs not as source code. The customers are free to create new Code and attach to the UI but they cannot change the existing UI through code. We are having a designer which will display the selectable, displayable properties from which the customer can select to display data to user. This way they can change the look and feel of the UI. Also they can change the binding property using this model.

如果我给了这片的问题,我可能会得到一些答案,可以给你的,是完全发生一个清晰的概念。

If i had given this piece in the question i might have got some more answer and might given you a clear idea on what exactly is happening.

通过上面的一块,下面是我的新思路

With the above piece, below is my new thought

我还想到一些使用codeDOM模型来创建单独的类并迁移到同一用户界面。下面是我的想法。

I am also thinking something of using CodeDom model to create the classes seperately and migrating the same to UI. Below is my thought

创建读取我们的结构的XML文件中的控制台应用程序。它将包含在系统中可用的属性和类。还创建一个用户界面来操作XML文件来创建类。在控制台中使用codeDOM生成的类和方法。使用库或shell命令编译它们。将创建类的UI,​​现在的用户界面将recogonize新类和可以显示其属性。

Create a console application which reads a XML file of our structure. it will contains the properties and classes that are available in the system. ALso create an UI to manipulate the XML file to create classes. in the console Use COdeDom to generate the classes and methods. Compile them using libraries or shell commands. Move the created classes to UI, Now UI will recogonize the new classes and can display the properties.

这可能不会产生任何的内存占用和高内存使用率/泄漏。同时,我们要更换这是简单的DLL。唯一的问题是类生成。它必须被sophasticated的方式团队必须能够在此UI进行任何操作。你们怎么认为

This might not create any memory hogging and high memory utilization/leaks. also we are replacing the DLLs which is simple. Only problem is the class generation. it must be sophasticated in a way the team must be able to perform any operation in this UI. what do you guys think

推荐答案

尝试使用MSIL工作

public class RunTimeObject<T> where T : class
{

    void EmitGetter(MethodBuilder methodBuilder, FieldBuilder fieldBuilder)
    {
        ILGenerator ilGenerator = methodBuilder.GetILGenerator();
        ilGenerator.Emit(OpCodes.Ldarg_0);
        ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
        ilGenerator.Emit(OpCodes.Ret);
    }

    void EmitSetter(MethodBuilder methodBuilder, FieldBuilder fieldBuilder)
    {
        ILGenerator ilGenerator = methodBuilder.GetILGenerator();
        ilGenerator.Emit(OpCodes.Ldarg_0);
        ilGenerator.Emit(OpCodes.Ldarg_1);
        ilGenerator.Emit(OpCodes.Stfld, fieldBuilder);
        ilGenerator.Emit(OpCodes.Ret);
    }

    public object CreateNewObject(T obj)
    {

        AssemblyName assemblyName = new AssemblyName { Name = "assembly" };
        AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("module");
        TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public | TypeAttributes.Class);

        foreach (var prop in obj.GetType().GetProperties())
        {
            FieldBuilder field = typeBuilder.DefineField("_" + prop.Name, typeof(string), FieldAttributes.Private);

            PropertyBuilder propertyBuilder =
                typeBuilder.DefineProperty(prop.Name,
                                 PropertyAttributes.None,
                                 typeof(string),
                                 new Type[] { typeof(string) });

            MethodAttributes methodAttributes =
                MethodAttributes.Public |
                MethodAttributes.HideBySig;

            MethodBuilder methodBuilderGetter =
                typeBuilder.DefineMethod("get_value",
                                           methodAttributes,
                                           typeof(string),
                                           Type.EmptyTypes);

            EmitGetter(methodBuilderGetter, field);


            MethodBuilder methodBuilderSetter =
                typeBuilder.DefineMethod("set_value",
                                           methodAttributes,
                                           null,
                                           new Type[] { typeof(string) });

            EmitSetter(methodBuilderSetter, field);


            propertyBuilder.SetGetMethod(methodBuilderGetter);
            propertyBuilder.SetSetMethod(methodBuilderSetter);
        }

        Type dynamicType = typeBuilder.CreateType();

        var dynamicObject = Activator.CreateInstance(dynamicType);

        var properties = dynamicType.GetProperties();

        int count = 0;

        foreach (var item in obj.GetType().GetProperties())
            properties[count++].SetValue(dynamicObject, item.GetValue(obj, null), null);

        return dynamicObject;

    }
}