Converting types from string to custom type

I have a string type that must be assigned to the type owner. ' User' My method GetFullNamereturns a name in the format ` string`, and I need to assign it to the owner of the type ' User'

def.Owner = uf.GetFullName(row["assignedto"].ToString());

Any suggestions would be helpful,

+3
source share
4 answers

So you need something like:

public class User
{
    ...

    public static implicit operator User(string x)
    {
        return new User(x);
    }
}

Personally, I'm not a fan of implicit conversions. You say you “need” to assign it that way ... what's wrong with an explicit constructor or a call to a static method? Or perhaps the extension method ( ToUser) on string?

+3
source

@Jon , , . , , , , , , /. .

+1

, , User.FromString( s), . ,

+1

/ .

0

Source: https://habr.com/ru/post/1755749/


All Articles