Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# language issue: when you need to serialize/deserialize a user defined Func/Action

c# language issue: when you need to serialize/deserialize a user defined Func/Action

Scheduled Pinned Locked Moved C#
csharpsecuritytoolsarchitecturehelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BillWoodruff
    wrote on last edited by
    #1

    okay, we know that's not possible via the usual save/restore techniques for damn good reasons: 1) Func and Action (delegates) can have references to external objects/states the compiler can't resolve. 2) if that could be done, it would be an ideal way to insert/execute malicious code. as i see it, now, that leaves two alternatives: 1) have some kind of plug-in architecture that exposes pre-defined delegates. but, that could also be vulnerable to security threats. more critical would be the delegates defined in the plug-in need for access/reference to the definitions/references of internal objects in the app it would manipulate. 2) a DSL/script-language that could be saved/restored as text, and then compiled via Roslyn reflection/emit, etc. a concrete example of the type of Func i'd iike to save:

    private static readonly Random rand = new Random();

    private double IntensityFunc(Edge edge)
    {
    var hour = DateTime.Now.Hour;

    if (hour > 3 && hour < 12) return edge.IntensityMin;
    
    if (hour > 22) return edge.IntensityMax;
    
    return rand.NextDouble() \* (edge.IntensityMax - edge.IntensityMin) + edge.IntensityMin;
    

    }

    mini-example showing Node and Edge definitions, and Func usage:

    // node or vertex
    public class Node: INode // interface not shown
    {
    public Node(string name, int id)
    {
    Name = name;
    Id = id;
    }

    public string Name { set; get; }
    
    public string Id { set; get; }
    

    }

    // an edge describes a connection between two nodes
    public class Edge: IEdge // interface not shown
    {
    public Edge(Node node1, Node node2, double intensity, double intensityMin = 0.0, double intensityMax = 0.0,
    Func intensityFunc = null)
    {
    Node1 = node1;
    Node2 = node2;
    Intensity = intensity;
    IntensityMin = intensityMin;
    IntensityMax = intensityMax;
    IntensityFunc = intensityFunc;
    }

    public Node Node1 { set; get; }
    
    public Node Node2 { set; get; }
    
    public double Intensity { set; get; }
    
    public double IntensityMin { set; get; }
    
    public double IntensityMax { set; get; }
    
    public Func IntensityFunc { set; get; }
    
    public double GetIntensity()
    {
        return IntensityFunc?.Invoke(this) ?? Intensity;
    }
    

    }

    // usage example
    private void Form1_Load(object sender, EventArgs e)
    {
    var Jack = new Node("Jack", 1);

    var Jill = new Node("Jill", 2);
    
    var JackToJill = new Edge(Jac
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups