C # mimic overrides assignment operator (=)

I have a little problem with a slightly simple wrapper class that I have.

It looks something like this:

public class Wrapper<T>
{
  private T _value;

  public Wrapper<T>(T value)
  {
    _value = value;
  }

  public static implicit operator Wrapper<T>(T value)
  {
    return new Wrapper<T>(value);
  }

  public static implicit operator T(Wrapper<T> value)
  {
    return value._value;
  }
}

I redefine implicit converters from and to T, so it behaves almost like an instance of T.

eg.

Wrapper<int> foo = 42;

However, I have a slight problem when assigning one instance of Wrapper to another, since I only want to assign the value of the second Wrapper class.

So, I have to do this:

Wrapper<int> foo = 42;
Wrapper<int> bar = (int)foo;

Or publish publicity through a property.

However, since this is in the library, and I don't want the user to depend on remembering this, do you guys know how I could simulate overriding the assignment operator?

( ) , Wrapper, , .

, , , - , : -)

+5
8

, . - , , , - .

- :

public Wrapper(Wrapper<T> w)
{
    _value = w._value;
}

:

Wrapper<int> foo = 42;
Wrapper<int> bar = new Wrapper<int>(foo);

, , .

Clone ( ICloneable), :

Wrapper<int> bar = foo.Clone();

, . . .

+5

Wrapper <T> a struct. , .

+3

Nullable <T> ... , , .Value.

( ) , Wrapper, , .

, , ? , , CLR .

+1

. , . , . Value .

, Wrapper , - .

0

, struct , , , .

, , .

, , (130 ) , ? ( , )

, : -/

, .

, 2 : CustomerTable UserTable:

public class CustomerTable
{
  Wrapper<string> Name;
}

public class UserTable
{
  Wrapper<string> Name;
}

, - :

CustomerTable customer = new CustomerTable();
UserTable user = new UserTable();
user.Name = customer.Name; // This breaks my internal dictionary

, , :

user.Name = (string)customer.Name;

, , ?

Value,

user.Name = customer.Name.Value; // or user.Name.Value = ....

, , : , .

, , , ( , /, ). .

- , .

0

, .

public class DBValue<T>
{
    public static implicit operator DBValue <T>(T value)
    {
         return new DBValue<T>(value);
    }

    public static explicit operator T(DBValue <T> dbValue)
    {
         return dbValue.Value;
    }

    private readonly T _value;
    public T Value { get { this._value; } }

    public DBValue(T value)
    {
         this._value = value;
    }
}

DBValue<T> T - ( , , ), . , DBValue<T> T, , T.

, , : DBValue T , ( ) , ?

, :

string value = MyProperty.Value

string value = (string)MyProperty

string value = MyProperty

... , , .

EDIT:

, - , - .

0

A J Lane , , , - .

DbValue T , T.

literalSomething.Text = Server.HtmlEncode(SomeTable.SomeStringColumn);

literalSomething.Text = Server.HtmlEncode((string)SomeTable.SomeStringColumn);

, .

, , , , .

, , , , , .

DbValue:

if (someValue.Value.HasValue) // someValue is DbValue<int?>

- , , "" , , , , .

, " ".

, , Value , , , .

: -)

0

, . , , = , # "", ... . Steffen Wrapper , . , .


Wrapper<T> , , :

public class CustomerTable
{
    private Wrapper<string> _Name;
    public Wrapper<string> Name {
        get { return _Name; }
        set { _Name = (string)value; }
    }
}

public class UserTable
{
    private Wrapper<string> _Name;
    public Wrapper<string> Name {
        get { return _Name; }
        set { _Name = (string)value; }
    }
}

If this change were made, it would not violate the existing code, since it still allows various ways to set the property:

CustomerTable customer = new CustomerTable();
UserTable user = new UserTable();

user.Name = customer.Name; //*** No longer breaks internal data structures

user.Name = "string literal";  // Works as expected with implicit cast operator
user.Name = (string)customer.Name; // Still allowed with explicit/implicit cast operator
user.Name = customer.Name.Value; // Also works if Value property is still defined

Since this still does not answer the original question, using the Wrapper class can be problematic if it is used outside the context of a class property, i.e. passed between an object, etc. Perhaps the entire Wrapper class could be eliminated with the correct class, including using a set of properties / accesses.

0
source

All Articles