简体   繁体   中英

How can I take a C# Class, as a string UserInput, and make it available to the ASP.NET Core runtime?

I have users who save data into a propertybag JSON store and want to have a customer-supplied C# Model file (like below), to be pasted in, and then some ( System.* wizardry) and make that class available on either a per-usersession or global form.

It's OK if I need to restart ASP.NET, ILInject, or anything else. My goal is to have this class work with hard-coded EntityFramework (if data layer), RazorPages (if display layer), Netwonsoft in memory, or other reasons.

How can I take a class like the one below, as a string, and add it dynamically to ASP.NET Core ? RazorPages, etc, at runtime?

public class Movie
{
    public int ID { get; set; }

    #region snippet11
    [StringLength(60, MinimumLength = 3)]
    [Required]
    public string Title { get; set; }
    #endregion

    #region snippet2
    [Display(Name = "Release Date")]
    [DataType(DataType.Date)]
    public DateTime ReleaseDate { get; set; }

    [Range(1, 100)]
    [DataType(DataType.Currency)]
    [Column(TypeName = "decimal(18, 2)")]
    public decimal Price { get; set; }
    #endregion

    [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
    [Required]
    [StringLength(30)]
    public string Genre { get; set; }

    [RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
    [StringLength(5)]
    [Required]
    public string Rating { get; set; }
}

First, possible you can't accomplish this without risking integrity has hell, as you are giving them an open way to get to your code. Second, as C# must be compiled, you'll have to push new versions every time your users adds or changes any of this classes (also applies to the necessary changes that the database to which your app is related to, due to the use of EntityFramework)

So, I'd advice you, that don't do something like that.

If you want to have a custom properties, try to think a "Master" class handler, something like a big class with as many fields as you think they may get to need key, field1-field50, or what ever. And 2 other tables, like tableDescription that related with key wil l tell some more info about that pseudoTable, a name and a clientId that will use it (if you share tables), and in other table that relates pseudoTable with the bigtablehandler named columns with types, so "key, pseudoTable.key, fieldN, type, columnDisplayName)

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM