简体   繁体   中英

GUID for System provided workflow activities in C#

Is there a way to have a guid for system activities in re-hosted workflow designer?

I would like to have a GUID for each and every activity I create and also for the system provided (assign, for, switch, etc,).

I have read that IAttachedPropertyStore is the way to go but none of the system provided activties implement these I think. http://blogs.msdn.com/b/bursteg/archive/2009/05/18/xaml-in-net-4-0-attached-properties-iattachedpropertystore-and-attachablepropertyservices.aspx

Any suggestion or ideas is welcome. These guids are necessary to import these flows into other tools and be able to modify them.

Please do ask if any other information is required to answer this.

If all you need is a 1:1 map from Activity to Guid, consider just hashing the Assembly Qualified name. With such a small set of inputs, I would not guess you would get collisions in a 16 byte hash.

var targetAssemblies = new[] { typeof(Activity).Assembly };
var knownActivities = new Dictionary<Guid, Type>();
var baseType = typeof(Activity);
var sha1 = new SHA1CryptoServiceProvider();

foreach (var t in targetAssemblies.SelectMany(a => a.GetTypes()))
{
    if (t.IsPublic && !t.IsAbstract && baseType.IsAssignableFrom(t))
    {
        var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(t.AssemblyQualifiedName));

        // hash is 20 bytes long, we are truncating it to 16 to fit the guid
        var guidData = new byte[16];
        Buffer.BlockCopy(hash, 0, guidData, 0, 16);
        var typeGuid = new Guid(hash.Take(16).ToArray());

        if (knownActivities.ContainsKey(typeGuid))
        {
            throw new InvalidOperationException("Collision");
        }
        knownActivities.Add(typeGuid, t);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM