Get the App
SLTechnology News&Howtos  ›  Database  › 

The extension method of C# and the customization of OA Credit disk platform

Shulou Source: shulou.com Published: 2022-06-01 04:58:28 09月27日 Update

The following is illustrated by a code example

Define a Npc class, OA credit tray platform customization (enterprise E: 217 1793)

/ / define the Npc class

Public class Npc

{

/ / define field variables

Private int npcId

Private string name

Public Npc () {}

/ / define the properties corresponding to field variables

Public int NpcID

{

Get {return npcId;}

Set {npcId = value;}

}

Public string Name

{

Get {return name;}

Set {name = value;}

}

}

When you use the Npc class, you want to call a method that sets both NpcId and Name, but you don't write such a method when defining the Npc class. What to do, you can add such a method directly to the Npc class, or you can derive a subclass from the Npc class, and then add a method that meets the requirements in the subclass. Another way is to extend a method directly into the Npc class as we're going to talk about today.

The following code extends a new method directly to the Npc class

/ / extend the Npc class

Public static class ExtendNpc

{

/ / extend a method to the Npc class

/ / SetIDName (this Npc npc, int id, string name)

Public static void SetIDName (this Npc npc, int id, string name)

{

Npc.NpcID = id

Npc.Name = name

}

}

Tags: Methods code variables subclasses fields time credit platform examples and at the same time properties what to do to say requirements C # Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Xiaomi Shulou Technology Linux