Why can't I set “this” to a value in C#?

筅森魡賤 提交于 2019-12-29 02:05:48

问题


I am using Dapper.net Extensions and I would like to be able to retrieve a Photo object and set 'this' to it without having to set each property individually. What would be the best way to accomplish this? In the code below it says I cannot assign to 'this' because it is readonly.

public class Photo
{
    public Int32 PhotoId { get; set; }
    public Guid ObjectKey { get; set; }
    public Int16 Width { get; set; }
    public Int16 Height { get; set; }
    public EntityObjectStatus ObjectStatus { get; set; }
    public PhotoObjectType PhotoType { get; set; }
    public PhotoFormat2 ImageFormat { get; set; }
    public Int32 CategoryId { get; set; }

    public Photo(int pPhotoId)
    {
        Load(pPhotoId);
    }

    public void Load(int pPhotoId)
    {
        using (SqlConnection conn = new SqlConnection(Settings.Conn))
        {
            conn.Open();
            this = conn.Get<Photo>(pPhotoId);
        }
    }
}

回答1:


Unfortunately, there is no way to do this without setting the properties. An elegant way to do this would be to use a static method to load the Photo. I don't have the extension you are using, so the following code example is a bit different, but it should work as an example.

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Photo
    {
        public Int32 PhotoId { get; set; }
        public Guid ObjectKey { get; set; }
        public Int16 Width { get; set; }
        public Int16 Height { get; set; }
        public Int32 CategoryId { get; set; }

        public static Photo Load(int id)
        {
            using (SqlConnection conn = new SqlConnection("ABC"))
            {
                return conn.Get<Photo>(id);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Photo photo = Photo.Load(1);
        }
    }
}

Some more discussion here from Jon Skeet about the topic: http://bytes.com/topic/c-sharp/answers/513887-cannot-assign-because-read-only




回答2:


You can't, you must copy the methods individually, however you can use methods like reflection or a library like AutoMapper to make it easier to do.

That being said, I think a better plan is make Load static and have it return a new Photo instance, this is the pattern you see most often in the .NET framework itself.

public static Photo Load(int pPhotoId)
{
    using (SqlConnection conn = new SqlConnection(Settings.Conn))
    {
        conn.Open();
        return conn.Get<Photo>(pPhotoId);
    }
}



回答3:


this is read-only ... so no, you can't do that.

There are frameworks like AutoMapper for mapping between objects. Perhaps you should look into that.

That being said.. I think your design could use a re-think. You've reached the point where your domain objects are loading data themselves and you've realised you'll be writing repetitive mapping code. I think it's time to extract this into a "Service" class and remove the logic from your domain objects altogether (thereby, making your question void, since you wouldn't encounter that situation anyway).



来源:https://stackoverflow.com/questions/18285087/why-cant-i-set-this-to-a-value-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!