C# generic string parse to any object

后端 未结 4 517
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 04:25

I am storing object values in strings e.g.,

string[] values = new string[] { \"213.4\", \"10\", \"hello\", \"MyValue\"};

is there any way to ge

4条回答
  •  甜味超标
    2021-02-01 04:54

    I know this doesn't answer your question, but have you looked at Dapper micro ORM?
    It's über-simple (compared to LINQ to SQL or, for that reason, Entity Framework) and does what you want.

    Consider this:

    public class Dog
    {
        public int? Age { get; set; }
        public Guid Id { get; set; }
        public string Name { get; set; }
        public float? Weight { get; set; }    
    }            
    
    var guid = Guid.NewGuid();
    var dog = connection.Query("select * from Dogs where Id = @Id",
        new { Id = 42 }
    ).First();
    

    Dapper itself is packaged in a single file and, reportedly, is used by StackOverflow team (apart from Linq to SQL).

提交回复
热议问题