Click here to Skip to main content
14,773,940 members
Home » Discussions » C#
   

C#

 
GeneralRe: c# programing Pin
OriginalGriff9-Dec-20 6:20
mveOriginalGriff9-Dec-20 6:20 
GeneralRe: c# programing Pin
CHill609-Dec-20 7:00
mveCHill609-Dec-20 7:00 
AnswerRe: c# programing Pin
Gerry Schmitz9-Dec-20 6:48
mveGerry Schmitz9-Dec-20 6:48 
AnswerRe: c# programing Pin
Pete O'Hanlon9-Dec-20 22:52
mvePete O'Hanlon9-Dec-20 22:52 
QuestionSend GMail Pin
Kevin Marois7-Dec-20 17:41
professionalKevin Marois7-Dec-20 17:41 
AnswerRe: Send GMail Pin
OriginalGriff7-Dec-20 22:04
mveOriginalGriff7-Dec-20 22:04 
AnswerRe: Send GMail Pin
Richard Deeming7-Dec-20 22:55
mveRichard Deeming7-Dec-20 22:55 
Questionc# language issue: when you need to serialize/deserialize a user defined Func/Action Pin
BillWoodruff7-Dec-20 3:40
mveBillWoodruff7-Dec-20 3:40 
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<Edge, double> 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<Edge, double> 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(Jack, Jill, 50.0, 20.0, 80.0);

    var jintensity = JackToJill.Intensity;

    JackToJill.IntensityFunc = IntensityFunc;

    // test intensity func
    for (var i = 0; i < 20; i++)
    {
        var jintensity2 = JackToJill.GetIntensity();
        Console.WriteLine(jintensity2);
    }
}

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

«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali


modified 7-Dec-20 18:15pm.

QuestionPlatform dependency of thread management Pin
Bernhard Hiller7-Dec-20 0:30
MemberBernhard Hiller7-Dec-20 0:30 
AnswerRe: Platform dependency of thread management Pin
Dave Kreskowiak7-Dec-20 4:59
mveDave Kreskowiak7-Dec-20 4:59 
QuestionC# NET. Framework program (area 2x) Pin
Member 150126776-Dec-20 0:49
MemberMember 150126776-Dec-20 0:49 
AnswerRe: C# NET. Framework program (area 2x) Pin
Richard MacCutchan6-Dec-20 2:28
mveRichard MacCutchan6-Dec-20 2:28 
AnswerRe: C# NET. Framework program (area 2x) Pin
BillWoodruff7-Dec-20 8:07
mveBillWoodruff7-Dec-20 8:07 
QuestionDecimal numbers in NET. Framework Pin
Member 150126775-Dec-20 3:48
MemberMember 150126775-Dec-20 3:48 
AnswerRe: Decimal numbers in NET. Framework Pin
OriginalGriff5-Dec-20 3:53
mveOriginalGriff5-Dec-20 3:53 
QuestionC# NET. Framework program Pin
Member 150126775-Dec-20 1:59
MemberMember 150126775-Dec-20 1:59 
AnswerRe: C# NET. Framework program Pin
OriginalGriff5-Dec-20 2:17
mveOriginalGriff5-Dec-20 2:17 
GeneralRe: C# NET. Framework program Pin
Member 150126775-Dec-20 2:26
MemberMember 150126775-Dec-20 2:26 
GeneralRe: C# NET. Framework program Pin
Ralf Meier5-Dec-20 2:31
professionalRalf Meier5-Dec-20 2:31 
GeneralRe: C# NET. Framework program Pin
Member 150126775-Dec-20 2:33
MemberMember 150126775-Dec-20 2:33 
GeneralRe: C# NET. Framework program Pin
OriginalGriff5-Dec-20 2:41
mveOriginalGriff5-Dec-20 2:41 
GeneralRe: C# NET. Framework program Pin
OriginalGriff5-Dec-20 2:37
mveOriginalGriff5-Dec-20 2:37 
QuestionHow to capture and print panel content into .pdf Pin
RedPandinus3-Dec-20 12:25
MemberRedPandinus3-Dec-20 12:25 
AnswerRe: How to capture and print panel content into .pdf Pin
Sandeep Mewara3-Dec-20 18:33
mvaSandeep Mewara3-Dec-20 18:33 
GeneralRe: How to capture and print panel content into .pdf Pin
RedPandinus4-Dec-20 3:01
MemberRedPandinus4-Dec-20 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.