为什么我不能设置"这&Q​​UOT;在C#中的价值? [英] Why can't I set "this" to a value in C#?

查看:259
本文介绍了为什么我不能设置"这&Q​​UOT;在C#中的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Dapper.net扩展,我想能够检索到它的照片对象,并设置这个,而不必单独设置每个属性。什么是完成这一任务的最佳方式?在下面的代码它说我不能分配给'这',因为它是只读的。

 公共类照片
{
公众的Int32 PHOTOID {搞定;组; }
公众的Guid ObjectKey {搞定;组; }
公共Int16的宽度{搞定;组; }
公众的Int16身高{搞定;组; }
公共EntityObjectStatus ObjectStatus {搞定;组; }
公共PhotoObjectType照排机{搞定;组; }
公共PhotoFormat2的imageformat {搞定;组; }
公众的Int32的CategoryId {搞定;组; }

公开的照片(INT pPhotoId)
{
负载(pPhotoId);
}

公共无效负载(INT pPhotoId)使用(SqlConnection的康恩=新的SqlConnection(Settings.Conn))
{$ B $
{
b conn.Open();
=这与conn.Get LT;照片>(pPhotoId);
}
}
}


解决方案

不幸的是,有没有办法做到这一点不设置属性。优雅的方式做,这是使用一个静态方法来加载照片。我没有你所使用的扩展,所以下面的代码示例是一个有点不同,但它应该作为一个例子工作。

 使用系统; 
使用System.Collections.Generic;
使用System.Data.SqlClient的;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;

命名ConsoleApplication1
{
公共类照片
{
公众的Int32 PHOTOID {搞定;组; }
公众的Guid ObjectKey {搞定;组; }
公共Int16的宽度{搞定;组; }
公众的Int16身高{搞定;组; }
公众的Int32的CategoryId {搞定;组; }

公共静态照片负荷(INT ID)
{使用(SqlConnection的康恩=新的SqlConnection(ABC))
{
返回康恩
不用彷徨<照片及GT;(ID);
}
}
}
类节目
{
静态无效的主要(字串[] args)
{
写真照片= Photo.Load(1);
}
}
}



乔恩一些更多的讨论在这里飞碟双向的话题:的http://字节。 COM /主题/升C /答案/ 513887-不能指派,因为只读的


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);
        }
    }
}

解决方案

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

这篇关于为什么我不能设置&QUOT;这&Q​​UOT;在C#中的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆